Python类返回“无”(不理想)

时间:2018-07-10 13:35:34

标签: python

当birthframe为0时,一切正常(不获取任何None),但是当其大于0(如0.6初始帧)为“ None”时,我不希望Class返回。

我在调用ClassNumberAnimation时得到“无”帧。我想隐藏'None',但是在函数(在下面查找)print "newval", (newval + noise_val)中,'None'语句没有返回(期望)。我希望班级在我正式致电班级时不返回“无”(请参阅​​下面的极端方式:我如何称呼班级)。我正在将Python3代码移植到Python 2.7中,这可能是导致我获得奇怪结果的一个因素。

from backports.functools_lru_cache import lru_cache

from vectortween.Animation import Animation
from vectortween.Mapping import Mapping
from vectortween.Tween import Tween


class NumberAnimation(Animation):
    """
    class to animate the value of a number between startframe and stopframe
    tweening optionally can be applied (default is None, which means linear animation)
    """

    def __init__(self, frm, to, tween=None, noise_fn=None):
        """

        :param frm: start value 
        :param to: end value
        :param tween: optional tweening function (default: linear)
        :param noise_fn: optional noise function (default: None)
          noise_fn needs to accept two parameters: a value (frm <= value <= to) and a time (0 <= time <= 1)
          if the noise_fn uses parameter t the noise will be animated in time; by accepting but ignoring t,
          the noise is only spatial
        """
        super(NumberAnimation, self).__init__(frm, to)
        if tween is None:
            tween = ['linear']
        self.noise_fn = noise_fn
        self.T = Tween(*tween)

    #@lru_cache(maxsize=1000)
    def make_frame(self, frame, birthframe, startframe, stopframe, deathframe, noiseframe=None):
        """
        animation happens between startframe and stopframe
        the value is None before aliveframe, and after deathframe
         * if aliveframe is not specified it defaults to startframe
         * if deathframe is not specified it defaults to stopframe

        initial value is held from aliveframe to startframe

        final value is held from stopfrome to deathframe 
        """

        if birthframe is None:
            birthframe = startframe
        if deathframe is None:
            deathframe = stopframe
        if frame < birthframe:
            return None
        if frame > deathframe:
            return None
        if frame < startframe:
            return self.frm
        if frame > stopframe:
            return self.to

        t = self.T.tween2(frame, startframe, stopframe)
        newval = Mapping.linlin(t, 0, 1, self.frm, self.to)
        if self.noise_fn is not None:
            if noiseframe is not None:
                nf = noiseframe
            else:
                nf = t
            noise_val = self.noise_fn(newval, nf)
        else:
            noise_val = 0

        print "newval", (newval + noise_val)
        return newval + noise_val

我在这里调用Class:但是,print语句返回“ None”,我不想返回该值。

n = NumberAnimation(frm=0, to=110, tween=["easeOutBounce"])

for i in range(100):

    print (n.make_frame(frame=i/20, birthframe=0.6, startframe=0, 
                        stopframe=5, deathframe=5))

我得到这样的结果:

None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
newval 33.275
33.275
newval 33.275
33.275
newval 33.275
33.275
newval 33.275
33.275
newval 33.275
33.275
newval 33.275

1 个答案:

答案 0 :(得分:0)

在两种情况下,您的方法make_frame实际上返回None(这就是您获得None的原因)

if frame < birthframe:
    return None
if frame > deathframe:
    return None

如果您不想看到“无”,则在这种情况下只需返回其他内容(例如空字符串''