python 有类似 C 语言的 using 关键字吗?
在 C++ 中,您通常可以通过仔细使用“using”关键字来显着提高代码的可读性,例如:
void foo()
{
std::vector< std::map <int, std::string> > crazyVector;
std::cout << crazyVector[0].begin()->first;
}
变得
void foo()
{
using namespace std; // limited in scope to foo
vector< map <int, string> > crazyVector;
cout << crazyVector[0].begin()->first;
}
Python 是否存在类似的东西,或者我必须完全限定所有内容?
我将添加免责声明,我知道使用有其陷阱,并且应该适当限制其范围。
In C++ you can often drastically improve the readability of your code by careful usage of the "using" keyword, for example:
void foo()
{
std::vector< std::map <int, std::string> > crazyVector;
std::cout << crazyVector[0].begin()->first;
}
becomes
void foo()
{
using namespace std; // limited in scope to foo
vector< map <int, string> > crazyVector;
cout << crazyVector[0].begin()->first;
}
Does something similar exist for python, or do I have to fully qualify everything?
I'll add the disclaimer that I know that using has its pitfalls and it should be appropriately limited in scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如 Bill 所说,Python 确实有结构,
但您也可以显式指定要从模块(命名空间)导入哪些名称:
这往往会使代码更具可读性/更容易理解,因为有人在源代码中看到标识符并不会这样做。不需要搜索所有导入的模块来查看它来自哪里。 这是一个相关的问题: 命名空间规范没有歧义
编辑< /em>:为了回应 Pax 的评论,我会提到你也可以写类似的东西
,但随后你需要写
而不是仅仅
这当然不一定是坏事。 我通常混合使用
from X import y
和import Xy
形式,只要我觉得这样能让我的代码最清晰。 在某种程度上,这当然是一个主观的事情。As Bill said, Python does have the construction
but you can also explicitly specify which names you want imported from the module (namespace):
This tends to make the code even more readable/easier to understand, since someone seeing an identifier in the source doesn't need to hunt through all imported modules to see where it comes from. Here's a related question: Namespace Specification In Absence of Ambuguity
EDIT: in response to Pax's comment, I'll mention that you can also write things like
but then you'll need to write
instead of just
This is not necessarily a bad thing, of course. I usually use a mixture of the
from X import y
andimport X.y
forms, whatever I feel makes my code clearest. It's certainly a subjective thing to some extent.当然,Python 的活力使得这一切变得微不足道。 如果你有一个类深埋在命名空间中:foo.bar.baz.blah,你可以这样做:
Sure, python's dynamism makes this trivial. If you had a class buried deep in a namespace: foo.bar.baz.blah, you can do:
或
或
其中 X 是您要使用的 Python 模块。
如果您向我们提供您认为需要清理的 Python 代码示例,将会很有帮助。
or
or
Where X is the Python module you want to use.
It would be helpful for you to give us a Python code sample that you think needs cleaned up.
除了大卫的回答之外:
from foo import bar as baz
中所示。我建议查找 import 关键字、
__import__ 的手册
内置和 sys.modules 的解释作为进一步阅读。In addition to David's answer:
from X import (foo, bar, blah)
for a sake of PEP8.from foo import bar as baz
.I recommend to look up the manual for the import keyword, the
__import__
built-in and explanation for sys.modules as further reading.Python 中的另一个选项是导入时的构造“as”。 例如:
这会将
foo.bar
作为baz
引入当前模块中,这允许实体在当前模块中获得不同的名称,可能是为了避免隐藏现有的当前模块中具有相同名称的实体。Another option in Python is the construct "as" when importing. For example:
This will bring
foo.bar
in asbaz
in the current module which allows the entity to get a different name in the current module, possibly to avoid hiding an existing entity in the current module with the same name.该方法
请注意,即使
bar
是foo
包中的模块, 也有效。 这使您可以限制名称空间污染,而无需在 foo.bar 中命名您可能想要使用的每个函数/类。 它还可以帮助代码的读者,因为他们会看到对bar.baz()
的调用,并更好地了解baz
来自何处。Note that
works even if
bar
is a module in thefoo
package. This lets you limit your namespace pollution without having to name each function/class infoo.bar
that you might care to use. It also aids readers of your code, because they'll see a call tobar.baz()
and have a better idea wherebaz
came from.