600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python 使用全局变量_如何在Python中的不同模块中使用全局变量

python 使用全局变量_如何在Python中的不同模块中使用全局变量

时间:2020-12-05 23:58:15

相关推荐

python 使用全局变量_如何在Python中的不同模块中使用全局变量

I investigated that scope of global variables in python is limited to the module. But I need the scope to be global among different modules. Is there such a thing? I played around __builtin__ but no luck.

thanks in advance!

解决方案

You can access global variables from other modules by importing them explicitly.

In module foo:

joe = 5

In module bar:

from foo import joe

print joe

Note that this isn't recommended, though. It's much better to hide access to a module's variables by using functions.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。