if __name__=='__main__':

这段代码下面的代码不会被其他模块运行,只能在自己的模块中运行

例:

a.py

def hello():

  print('Hello World!')

b.py

import a

a.hello()

#result:Hello World!

c.py

if __name__=='__main__':

  def hello():

    print('Hello World')

d.py

import c

c.hello()

#result:AttributeError: module 'c' has no attribute 'hello'

 posted on 2020-06-14 11:12  山有木兮木有枝0  阅读(189)  评论(0)    收藏  举报