python中__name__
if __name__=="__main()":
允许这个程序在独立运行的时候做些有用的事情,不会在别的程序将它做为模块使用而引起冲突,也防止别人引用该模块执行其中的代码
看例子:
def test(): print 'test is running!' if __name__=='__main__': print 'main is working' if __name__=='snail': print 'snail is working'?
F5:
>>> test()
test is running!
main is working
>>> import snail
>>> snail.__name__
'snail'
>>> test()
test is running!
main is working
>>> snail.test()
test is running!
snail is working
>>>
?