python - 为什么在 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.py
和mod2.py
作为单独的文件。
解决方案
list.append()
就地工作(并因此返回None
)。您将其分配回vidfilter
此行:
vidfilter = vidfilter.append("subtitles=/example/file/path/toptext.srt:force_style='Fontname=Impact,Fontsize=30,Alignment=6'")
另请注意,我不确定您认为这是在做什么:
vidfilter = vidfilter
推荐阅读
- android - 为什么 Kotlin 会在传递通用 Result 参数时崩溃?
- java - 在两个日期之间弹簧数据 jpa
- javascript - 根据列过滤antd表
- powershell - 为广告列表排除帐户
- python - 在 centOS 7 中安装 pip
- javascript - Bot 总是发送相同的响应 | 不和谐.js
- javascript - JavaScript将对象数组添加到特定键
- java - 使用 Apache Camel 对 *.bai.asc 文件进行 PGP 解密
- node.js - if-else 无法在 Typescript 中输入检查
- android - 如何创建一个从多个列表中获取属性的小部件列表?