我有这样的功能:
def ease_bounce_out(t, bounces=None):
if bounces is None:
bounces = [4 / 11, 6 / 11, 8 / 11, 3 / 4, 9 / 11, 10 / 11, 15 / 16, 21 / 22, 63 / 64]
bounces.insert(0, 1 / bounces[0] / bounces[0])
if t < bounces[1]:
return bounces[0] * t * t
else:
for i in range(3, len(bounces) - 2, 3):
if t < bounces[i]:
t -= bounces[i - 1]
return bounces[0] * t * t + bounces[i + 1]
t -= bounces[len(bounces) - 2]
return bounces[0] * t * t + bounces[len(bounces) - 1]
我想把它全部压缩成 1 个字符串,这样我就可以使用该eval()
函数来获取任何值的输出t
。我有一个更简单的功能示例:
def ease_poly(t, power=2):
t *= 2
if t < 1:
return t ** power
else:
return 2 - ((2 - t) ** power)
会成为:
def ease_poly(power=2):
return f"(t * 2) ** {power} if (t * 2) < 1 else 2 - ((2 - (t * 2)) ** {power})"
这样,我可以使用这个字符串并t
通过简单地执行以下操作来评估函数的任何值:
ease = ease_poly(power=3)
t = 0.4 # 0 <= t <= 1
print(eval(ease))
现在开始我的问题,它实际上不必是 1 行,这就是我一直在想的:
def ease_bounce_out(bounces=None):
if bounces is None:
bounces = [4 / 11, 6 / 11, 8 / 11, 3 / 4, 9 / 11, 10 / 11, 15 / 16, 21 / 22, 63 / 64]
return # some code here that compiles the rest into a string