# file one.py def func(): print("func() in one.py") print("top-level in one.py") if __name__ == "__main__": print("one.py is being run directly") else: print("one.py is being imported into another module") # file two.py import one print("top-level in two.py") one.func() if __name__ == "__main__": print("two.py is being run directly") else: print("two.py is being imported into another module")
小明.py
朋友眼中你是小明(__name__ == '小明'),
你自己眼中你是你自己(__name__ == '__main__'),
但你晚上也会打开xx网站, 做一些自己的事情(直接运行小明.py, __name__ == '__main__')