设置特殊变量,如__name__;css
执行源代码文件中的全部代码;python
print("before import")import math
print("before functionA")def functionA(): print("Function A")
print("before functionB")def functionB(): print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")if __name__ == '__main__': functionA() functionB()print("after __name__ guard")
python foo.py
__name__ = "__main__"
import foo
__name__ = "foo"
before importbefore functionAbefore functionBbefore __name__ guardFunction AFunction B 10.0after __name__ guard
before importbefore functionAbefore functionBbefore __name__ guardFunction AFunction B 10.0after __name__ guard
before importbefore functionAbefore functionBbefore __name__ guardafter __name__ guard
import fooprint('current module')
before importbefore functionAbefore functionBbefore __name__ guardafter __name__ guardcurrent module
# foo2.pydef functionA(): print("a1") import foo2 print("a2") functionB() print("a3")
def functionB(): print("b")
print("t1")if __name__ == "__main__": print("m1") functionA() print("m2")print("t2")
t1m1a1t1t2a2ba3m2t2
# foo3.pydef functionA(): print("a1") import foo3 print("a2") functionB() print("a3")
def functionB(): print("b")
print("t1")print("m1")functionA()print("m2")print("t2")
t1m1a1t1m1a1a2ba3m2t2a2ba3m2t2
一、连Python产生器(Generator)的原理都解释不了,还敢说Python用了5年?nginx
二、牛掰了!鸿蒙与Android完美融合,将鸿蒙设备当Android设备用web
四、【鸿蒙学院】鸿蒙IDE:下载、安装DevEco Studioswift
关注「极客起源」公众号,加星标,不错过精彩技术干货
app
本文分享自微信公众号 - 极客起源(geekculture)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。编程语言