【发布时间】:2020-11-19 19:22:35
【问题描述】:
如何在Start函数中调用WriteMessage函数?
我收到以下错误:发生异常:NameError
名称“WriteMessage”未定义
我的代码:
import sys,time,os
class Foo:
def Start(self):
message="Do something..."
WriteMessage(message)
name = input()
return name
def WriteMessage(self,message):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.1)
if __name__ == "__main__":
foo= Foo()
foo= foo.Start()
【问题讨论】:
-
旁注:Python 有一个official style guide。大写方法名称不是很惯用。我建议changing them to
startandwrite_message,以及moving each of your imports to separate lines。遵循 PEP 8 将使有经验的 Python 开发人员更容易阅读您的代码。
标签: python algorithm function methods self