python 有类似 C 语言的 using 关键字吗?

发布于 2024-07-14 02:18:50 字数 516 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧夏天 2024-07-21 02:18:50

正如 Bill 所说,Python 确实有结构,

from X import *

但您也可以显式指定要从模块(命名空间)导入哪些名称:

from X import foo, bar, blah

这往往会使代码更具可读性/更容易理解,因为有人在源代码中看到标识符并不会这样做。不需要搜索所有导入的模块来查看它来自哪里。 这是一个相关的问题: 命名空间规范没有歧义

编辑< /em>:为了回应 Pax 的评论,我会提到你也可以写类似的东西

import X.foo

,但随后你需要写

X.foo.moo()

而不是仅仅

foo.moo()

这当然不一定是坏事。 我通常混合使用 from X import yimport Xy 形式,只要我觉得这样能让我的代码最清晰。 在某种程度上,这当然是一个主观的事情。

As Bill said, Python does have the construction

from X import *

but you can also explicitly specify which names you want imported from the module (namespace):

from X import foo, bar, blah

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

import X.foo

but then you'll need to write

X.foo.moo()

instead of just

foo.moo()

This is not necessarily a bad thing, of course. I usually use a mixture of the from X import y and import X.y forms, whatever I feel makes my code clearest. It's certainly a subjective thing to some extent.

乖乖 2024-07-21 02:18:50

当然,Python 的活力使得这一切变得微不足道。 如果你有一个类深埋在命名空间中:foo.bar.baz.blah,你可以这样做:

def foo:
    f = foo.bar.baz.blah
    f1 = f()

Sure, python's dynamism makes this trivial. If you had a class buried deep in a namespace: foo.bar.baz.blah, you can do:

def foo:
    f = foo.bar.baz.blah
    f1 = f()
一绘本一梦想 2024-07-21 02:18:50
import X

from X import *

from X import a, b, c

其中 X 是您要使用的 Python 模块。

如果您向我们提供您认为需要清理的 Python 代码示例,将会很有帮助。

import X

or

from X import *

or

from X import a, b, c

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.

余生再见 2024-07-21 02:18:50

除了大卫的回答之外:

  1. 为了 PEP8
  2. 完整语法允许将包名称重命名(重新绑定)到当前模块范围内的新标识符,如 from foo import bar as baz 中所示。

我建议查找 import 关键字__import__ 的手册 内置和 sys.modules 的解释作为进一步阅读。

In addition to David's answer:

  1. One should use round brackets in from X import (foo, bar, blah) for a sake of PEP8.
  2. Full syntax allows renaming (rebinding) package names to their new identifiers within the scope of the current module as in 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.

別甾虛僞 2024-07-21 02:18:50

Python 中的另一个选项是导入时的构造“as”。 例如:

from foo import bar as baz

这会将 foo.bar 作为 baz 引入当前模块中,这允许实体在当前模块中获得不同的名称,可能是为了避免隐藏现有的当前模块中具有相同名称的实体。

Another option in Python is the construct "as" when importing. For example:

from foo import bar as baz

This will bring foo.bar in as baz 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.

半步萧音过轻尘 2024-07-21 02:18:50

该方法

from foo import bar

请注意,即使 barfoo 包中的模块, 也有效。 这使您可以限制名称空间污染,而无需在 foo.bar 中命名您可能想要使用的每个函数/类。 它还可以帮助代码的读者,因为他们会看到对 bar.baz() 的调用,并更好地了解 baz 来自何处。

Note that

from foo import bar

works even if bar is a module in the foo package. This lets you limit your namespace pollution without having to name each function/class in foo.bar that you might care to use. It also aids readers of your code, because they'll see a call to bar.baz() and have a better idea where baz came from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文