私信  •  关注

Peter Mortensen preetika monda

Peter Mortensen preetika monda 最近创建的主题
Peter Mortensen preetika monda 最近回复了
5 年前
回复了 Peter Mortensen preetika monda 创建的主题 » 如何使用uu name_uuuu==”uuuu main_uuu“来编写python脚本[副本]

如果 名称 == ' 主要 ':

我们看看 __name__ == '__main__': 经常。

它检查是否正在导入模块。

换句话说,在 if 只有当代码直接运行时才会执行块在这里 directly 方法 not imported .

让我们使用打印模块名称的简单代码来了解它的功能:

# test.py
def test():
   print('test module name=%s' %(__name__))

if __name__ == '__main__':
   print('call test()')
   test()

如果我们直接通过 python test.py ,模块名为 __main__ :

call test()
test module name=__main__