import modx 和 from modx import * 有什么区别? [英] What is the difference between import modx and from modx import *?

查看:24
本文介绍了import modx 和 from modx import * 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要导入一个名为 modx 的模块,这与说

If I were to import some module called modx, how would that be different from saying

from modx import *

不是所有的内容都从任何一种方式导入吗?这是在python中只是为了澄清.

Wouldn't all the contents be imported from each either way? This is in python just to clarify.

推荐答案

如果你 import somemodule 包含的全局变量将通过 somemodule.someglobal 可用.如果你 from somemodule import * 它的所有全局变量(或那些在 __all__ 中列出的,如果存在)将成为全局变量,即你可以使用 someglobal前面没有模块名称.

If you import somemodule the contained globals will be available via somemodule.someglobal. If you from somemodule import * ALL its globals (or those listed in __all__ if it exists) will be made globals, i.e. you can access them using someglobal without the module name in front of it.

不鼓励使用 from module import *,因为它会使全局范围变得混乱,并且如果您从多个模块导入内容,则可能会发生冲突并覆盖现有的类/函数.

Using from module import * is discouraged as it clutters the global scope and if you import stuff from multiple modules you are likely to get conflicts and overwrite existing classes/functions.

这篇关于import modx 和 from modx import * 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆