我试图找到一个关于是否最好使用的综合指南import module或from module import?我刚刚开始使用Python,我试图从最初的实践开始.
基本上,我希望如果有人能分享他们的经验,有什么喜好其他开发商,什么是避免任何的最好办法陷阱的道路?
只是对Python中的全局价值感到困惑,这里有两段代码
#gl.py
import cli
a = 1
print "gl 1: %d %d" % (id(a), a)
def reset():
global a
a = 7
print "reset 1: %d %d" % (id(a), a)
if __name__ == '__main__':
cli.handler(reset)
print "gl 2: %d %d" % (id(a), a)
Run Code Online (Sandbox Code Playgroud)
cli代码
#cli.py
def handler(func):
from gl import a
print "cli 1: %d %d" % (id(a), a)
func()
print "cli 2: %d %d" % (id(a), a)
Run Code Online (Sandbox Code Playgroud)
执行的结果是
$ python gl.py
gl 1: 150847672 1
gl 1: 150847672 1 …Run Code Online (Sandbox Code Playgroud)