【发布时间】:2020-04-30 15:00:38
【问题描述】:
我将类声明和主脚本放在同一个模块中,发现虽然没有调用或实例化'My'类,但执行了'print'子句。
有人可以帮我理解为什么虽然我没有调用类“My”,但为什么会输出“This will be run”?
class My():
print("This will be run")
def myout(self):
print("This will not be run")
if __name__ == '__main__':
print("Hello World")
预期输出:
你好世界
实际输出:
这将运行
你好世界
【问题讨论】:
-
如果您将语句 print("This will be run") 换成 x = max(1,2) 之类的语句,可能对您更有意义
-
请始终对所有与 python 相关的问题使用通用 python 标签
标签: python python-3.x