2016-11-21 74 views
1

我试图用in-line,lambda函数来执行一些if/else条件,我得到"unsupported operand type(s) for +: 'int' and 'function'"错误。在这里,我试图将set_duration设置为1.5,除非它的第一次和最后一次数组迭代。你可以看看并提供任何提示。期待。python lambda if/else条件失败:'int'和'function'冲突

for idx, string in enumerate(lines): 


    duration = 10 
    clips = [] 

    clips.append(ImageClip(os.path.join(folder,"gradient.png")) 
       .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5) 
       ) 

    clips.append(ImageClip(os.path.join(folder,"image-{0}.png")) 
       .resize(width=800) 
       .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5) 
       .margin(right=60, opacity=0) 
       .set_pos(("right","center")) 
       ) 

    clips.append(ImageClip(os.path.join(folder, "big-append.png")) 
       .resize(width=900) 
       .margin(left=60,opacity=0) 
       .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5) 
       .set_pos(("left", "center")) 
       ) 



// rest of the code... 

FullTraceback

Traceback (most recent call last): 
    File "C:\vhosts\phpsols\pymovie\FORMAT-4\three.py", line 90, in <module> 
    .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5) 
    File "<decorator-gen-29>", line 2, in set_duration 
    File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 29, in apply_to_mask 
    newclip = f(clip, *a, **k) 
    File "<decorator-gen-28>", line 2, in set_duration 
    File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 41, in apply_to_audio 
    newclip = f(clip, *a, **k) 
    File "<decorator-gen-27>", line 2, in set_duration 
    File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 89, in wrapper 
    return f(*new_a, **new_kw) 
    File "<decorator-gen-26>", line 2, in set_duration 
    File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 14, in outplace 
    f(newclip, *a, **k) 
    File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\Clip.py", line 288, in set_duration 
    self.end = None if (t is None) else (self.start + t) 
TypeError: unsupported operand type(s) for +: 'int' and 'function' 
Process terminated with an exit code of 1 
+0

编辑:对不起,只是一个盲目的猜测,试试看它适用于我...你确定错误指向到这部分代码中?你在这里没有“+”......错误基本上说你试图将INT和FUNCTION一起添加:)由于“不支持的操作数类型为+:'int'和'function ''并不是指你的lambda中的“和”。 –

+0

@PeterMajko,嗨不是迄今为止 –

+0

@PeterMajko,它的影片模块,所以ImageClip是否有标准类 –

回答

5

你并不需要一个lambda。只需直接设置持续时间。该值已经在每次循环迭代中重新计算。

   .set_duration(3.0 if idx == 0 and lines[-1] else 1.5) 
+0

我仍然希望看到错误背后的原因,即使这样的事情修复了用户的问题:) –

+0

编辑的问题(带回溯)现在显示问题不在这里...... – SiHa

+1

@PeterMajko :'.set_duration()'试图计算'self.start + t'(参见堆栈跟踪),其中't'是用户的输入,错误地是lambda(函数),'self.start'是一个整数。因此,'不支持的操作数类型为+:'int'和'function'错误。 – kennytm

0

在这种情况下,您根本不需要lambda。只需在set_duration()调用中输入“3 if(idx ...”)表达式