首页 > 解决方案 > 为什么在 2 个函数之间传递变量时,变量是 None 而不是?

问题描述

我正在制作一个基本的编辑软件。

部分代码的简化版本如下(它们仍然产生与实际程序相同的结果):

文件名为mod.py

from mod2 import toptext

cmdtext = "Edit video: tt=test,"

presentCommands = ['tt=']

def initCommands(presentCommands, cmdtext):
    vidfilter = []
    if 'tt=' in presentCommands and 'bt=' not in presentCommands:
        vidfilter = toptext(cmdtext, vidfilter)
        print("Top text vidfilter is", vidfilter)
    else:
        print('no')

initCommands(presentCommands, cmdtext)

文件名为mod2.py

import re

def toptext(cmdtext, vidfilter):
    vidfilter = vidfilter
    texts = (re.findall(r'tt=(.*?),', cmdtext))
    text = texts[0]
    print(text)
    vidfilter = vidfilter.append("subtitles=/example/file/path/toptext.srt:force_style='Fontname=Impact,Fontsize=30,Alignment=6'")
    print("The vidfilter is:", vidfilter)
    return vidfilter

运行的输出mod.py是这样的:

test
The vidfilter is: None
Top text vidfilter is None

如您所见, vidfilter 打印为None, 当我想要它时["subtitles=/example/file/path/toptext.srt:force_style='Fontname=Impact,Fontsize=30,Alignment=6'"]。是append()不工作还是什么?

谁能帮我解决这个问题,让我知道为什么会这样?请注意,我想保留mod.pymod2.py作为单独的文件。

标签: pythonpython-3.xlistvariables

解决方案


list.append()就地工作(并因此返回None)。您将其分配回vidfilter此行:

vidfilter = vidfilter.append("subtitles=/example/file/path/toptext.srt:force_style='Fontname=Impact,Fontsize=30,Alignment=6'")

另请注意,我不确定您认为这是在做什么:

vidfilter = vidfilter

推荐阅读