简体   繁体   English

如何从模块导入类列表?

[英]How do you import a list of classes from a module?

Is there a way to import only a specific, predefined list of classes from a Python module? 有没有办法从Python模块中仅导入特定的预定义类列表? For example, say that I had modules a.py and b.py in the same directory with the following code: 例如,假设我在同一目录中具有以下代码的模块a.pyb.py

#a.py
class Foo(object):
    '''definition of class Foo goes here'''

class Bar(object):
    '''definition of class Bar goes here'''

aTypes = [Foo, Bar]

_ _

#b.py
from a import aTypes

print Foo

Running b.py causes, of course, the print Foo line to raise a NameError . 当然,运行b.py会使print Foo行引发NameError I didn't really think that this would work (the import statement in b.py gives me an a.Foo type instead of a Foo type), but I can't figure out the right syntax. 我真的不认为这会起作用( b.py的import语句为我提供了a.Foo类型而不是Foo类型),但是我找不到正确的语法。 Is there an alternative to from a import aTypes that gives the desired behavior? 有没有替代from a import aTypes ,可以提供所需的行为?

You could import the names directly: 您可以直接导入名称:

from a import Foo, Bar

Alternately, you could define __all__ in a.py : 或者,您可以在a.py定义__all__

__all__ = ['Foo', 'Bar']

and then do a wildcard import in b.py : 然后在b.py进行通配符导入:

from a import *

For just two names though, it would be easier to use the first solution. 不过,仅使用两个名称,使用第一个解决方案会更容易。

Supposing you really do need to do this (deal with list of types) and can't use tricks like __all__ that only work once (one such special list per module), then: 假设您确实确实需要执行此操作(处理类型列表),并且不能使用仅工作一次的__all__之类的技巧(每个模块一个这样的特殊列表),然后:

def gettypes(types):
    thismodule = sys.modules[__name__]
    for t in types:
        setattr(thismodule, t.__name__, t)

Use it like this: 像这样使用它:

import a
gettypes(a.aTypes)

I'm finding it difficult to imagine why you'd need this, but that's not my problem ;-) 我很难想象您为什么需要这个,但这不是我的问题;-)

Note the use of __name__ means this doesn't work for symbols in general, only for classes, modules, functions and anything else I've forgotten (or at a pinch for objects of your own design that have a __name__ ). 请注意,使用__name__意味着这通常不适用于符号,仅适用于类,模块,函数和我忘记的任何其他内容(或者对于您自己设计的具有__name__对象也很__name__ )。 It also won't work for things you've aliased in the module, for example by writing Baz = Bar in the module and then using Baz in the list. 它也不适用于您在模块中别名的事情,例如,通过在模块中编写Baz = Bar ,然后在列表中使用Baz来实现。 If the list was ['Foo', 'Bar'] instead of [Foo, Bar] , and you also passed the source module into gettypes , then you could avoid those restrictions. 如果列表是['Foo', 'Bar']而不是[Foo, Bar] ,并且您还将源模块传递给gettypes ,则可以避免这些限制。

You can define __all__ at the module level of a.py : 您可以在a.py的模块级别定义__all__

__all__ = ['Foo', 'Bar']

This will allow you to import it in the following way: 这将使您可以通过以下方式导入它:

from a import *

Of course, this behaviour isn't always encouraged, and the alternative is preferred: 当然,并非总是鼓励这种行为,因此首选替代方法:

from a import Foo, Bar

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

相关问题 在python中,如何在不保留导入模块名称空间的情况下从其他模块导入所有类? - In python, how do you import all classes from another module without keeping the imported module's namespace? 从模块导入类列表 - Import a list of classes from a module Python - 如何将模块从另一个文件夹导入子文件夹? - Python - How do you import a module into a subfolder from another folder? 如何将模块中的所有类作为列表导入? - How to import all the classes in a module as a list? 如何从列表动态生成类? - How do you dynamically generate classes from a list? 您如何修补模拟从 class 导入到您正在测试的模块的导入? - How do you patch mock an import from a class that is being imported to a module you are testing? 如何获取从 Python 3 中的模块导入的所有类的列表? - How can I get a list of all classes I import from a module in Python 3? 如何导入一个模块,该模块会导入子文件夹中的另一个模块? - How do you import a module that imports another module in a subfolder? 如何导入和使用从其他文件中导入类的python模块? - How do I import and use a python module that imports classes from another file within itself? 如何将自定义类导入 Django views.py? - How do you import custom classes into Django views.py?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM