简体   繁体   English

python是否有类似C ++的using关键字的东西?

[英]Does python have something like C++'s using keyword?

In C++ you can often drastically improve the readability of your code by careful usage of the "using" keyword, for example: 在C ++中,通常可以通过谨慎使用“ using”关键字来大大提高代码的可读性,例如:

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? python是否存在类似的东西,还是我必须完全限定所有内容?

I'll add the disclaimer that I know that using has its pitfalls and it should be appropriately limited in scope. 我将添加一个免责声明,即我知道使用存在缺陷,应该在范围上进行适当限制。

As Bill said, Python does have the construction 正如Bill所说,Python确实具有

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 编辑 :针对Pax的评论,我会提到您还可以编写类似

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 Xy forms, whatever I feel makes my code clearest. 我通常将from X import yimport Xy两种形式混合使用,无论我觉得使我的代码最清晰。 It's certainly a subjective thing to some extent. 在某种程度上,这当然是主观的事情。

import X

or 要么

from X import *

or 要么

from X import a, b, c

Where X is the Python module you want to use. 其中X是您要使用的Python模块。

It would be helpful for you to give us a Python code sample that you think needs cleaned up. 为您提供一个您认为需要清理的Python代码示例会对您有所帮助。

Sure, python's dynamism makes this trivial. 当然,python的动态性使它变得微不足道。 If you had a class buried deep in a namespace: foo.bar.baz.blah, you can do: 如果您将一个类埋在名称空间foo.bar.baz.blah中,则可以执行以下操作:

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

Another option in Python is the construct "as" when importing. Python中的另一个选项是导入时的结构“ as”。 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. 这会将foo.bar作为baz引入当前模块中,这允许该实体在当前模块中获得不同的名称,这可能是为了避免在当前模块中隐藏具有相同名称的现有实体。

In addition to David's answer: 除了大卫的答案:

  1. One should use round brackets in from X import (foo, bar, blah) for a sake of PEP8 . 出于PEP8的考虑 from X import (foo, bar, blah)应该在from X import (foo, bar, blah)使用圆括号from X import (foo, bar, blah)
  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 . 完整的语法允许将软件包名称重命名(重新绑定)为当前模块范围内的新标识符,如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. 我建议您查阅手册以获取import关键字 ,内置的__import__sys.modules的说明,以进一步阅读。

Note that 注意

from foo import bar

works even if bar is a module in the foo package. 即使barfoo包中的模块,它也可以工作。 This lets you limit your namespace pollution without having to name each function/class in foo.bar that you might care to use. 这使您可以限制名称空间污染,而不必在foo.barfoo.bar您可能会想使用的每个函数/类。 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. 它还可以帮助您的代码读者,因为他们会看到对bar.baz()的调用,并且可以更好地了解baz来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM