简体   繁体   English

大多数pythonic方法导入模块中的所有对象作为模块中的名称

[英]Most pythonic way to import all objects in a module as their name in the module

When you import a module, python protects the namespace by importing all objects in that module as module.objectname instead of objectname . 导入模块时,python通过将该模块中的所有对象导入为module.objectname而不是objectname来保护命名空间。 import module.objectname as objectname will import the object as its original name in the module, but writing out every object in this manner would be tedious for a large module. import module.objectname as objectname导入将导入对象作为模块中的原始名称,但是以这种方式写出每个对象对于大型模块来说将是繁琐的。 What is the most pythonic way to import all objects in a module as their name within the module? 导入模块中所有对象作为模块中名称的最pythonic方法是什么?

This would import everything from modules as their name: 这将导入模块中的所有内容作为其名称:

from module import *

But it's not really good practice. 但这不是一个好的做法。 Import only what is really needed and use PEP8 tests for your code. 仅导入真正需要的内容并对代码使用PEP8测试。

You only need to use this form 您只需要使用此表单

import module.objectname as objectname

If you wish to alias the objectname to a different name 如果您希望将对象名别名为其他名称

Usually you say 通常你说

from module import objectname, objectname2, objectname3

There is no "Pythonic" way to import all the objects as from module import * is discouraged (causes fragile code) so can hardly be called Pythonic 没有“Pythonic”方法导入所有对象,因为不鼓励from module import *导致脆弱的代码,所以几乎不能称为Pythonic

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

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