1

Possible Duplicate:
What does if __name__=="__main__": do?
What's the point of a main function and/or __name__ == "__main__" check in Python?

I just wanted to understand why you have you use the __name__='__main__'statement if we can run any python script even without using that statement. For example, I can run the script below without using the if __name__='__main__' statement.

def hello():
      print "hello"
      return 1234

# And here is the function being used
print hello()
4

2 回答 2

5

It's done so that code is only executed when run as a script and not when you import the module.

于 2012-08-05T01:54:43.670 回答
0

全局命名空间中的代码运行速度稍慢。做一个函数很容易main(),为什么不做呢?如果您在导入模块时不介意模块“运行”,则它是可选的

于 2012-08-05T02:40:50.953 回答