在python中从name实例化一个类

时间:2022-12-23 12:46:12

I got the following files architecture:

我有以下文件架构:

[X] publisher/
    [X] __init__.py (containing Publisher(Object), an abstractclass)
    [X] mail.py (containing class Mail(Publisher))
    [X] simple.py (containing class Simple(Publisher))
    [X] ftp.py (containing class Ftp(Publisher))
[X] app.py

In app.py I got this dictionary :

在app.py中我得到了这本词典:

publisher_to_load = ["Mail","Mail","Simple"]

I would like to instanciate, for each publisher_to_load, the corresponding publisher.

我想为每个publisher_to_load实现相应的发布者。

I tried :

我试过了 :

import publisher
for name in publisher_to_load:
    getattr(publisher, name)()

But I got the error

但我得到了错误

AttributeError: 'module' object has no attribute 'Simple'

Any idea ?

任何的想法 ?

1 个解决方案

#1


As may have been mentioned, your problem is the imports in __init__.py. That should solve your problem. However, given the general title of the question I will also note a few more things for anyone else. First of all, if you do in fact think you have to have to make things import each other like in the question alex referenced, first consider a better option to make two programs cooperate. If you need two-way communication, open up a socket or design some other kind of intermediary hand-off of information, two programs importing each other is a truly messy way to make two modules cooperate. Secondly, accessing variable names with strings should be avoided if possible. Especially in this case, that is not how handling imports should work. That being said, gatattr (or object.__dict__[]) like the question uses should work just fine.

正如可能已经提到的,您的问题是__init__.py中的导入。那应该可以解决你的问题。但是,鉴于问题的一般标题,我还会为其他人注意一些事情。首先,如果你确实认为你必须像在亚历克斯引用的问题中那样互相导入事物,首先考虑一个更好的选择,使两个程序合作。如果你需要双向通信,打开一个套接字或设计一些其他类型的中介切换信息,两个程序相互导入是一个真正凌乱的方式,使两个模块合作。其次,如果可能,应避免使用字符串访问变量名。特别是在这种情况下,这不是处理进口应该如何工作。话虽这么说,gatattr(或对象.__ dict __ [])就像使用的问题应该工作得很好。

#1


As may have been mentioned, your problem is the imports in __init__.py. That should solve your problem. However, given the general title of the question I will also note a few more things for anyone else. First of all, if you do in fact think you have to have to make things import each other like in the question alex referenced, first consider a better option to make two programs cooperate. If you need two-way communication, open up a socket or design some other kind of intermediary hand-off of information, two programs importing each other is a truly messy way to make two modules cooperate. Secondly, accessing variable names with strings should be avoided if possible. Especially in this case, that is not how handling imports should work. That being said, gatattr (or object.__dict__[]) like the question uses should work just fine.

正如可能已经提到的,您的问题是__init__.py中的导入。那应该可以解决你的问题。但是,鉴于问题的一般标题,我还会为其他人注意一些事情。首先,如果你确实认为你必须像在亚历克斯引用的问题中那样互相导入事物,首先考虑一个更好的选择,使两个程序合作。如果你需要双向通信,打开一个套接字或设计一些其他类型的中介切换信息,两个程序相互导入是一个真正凌乱的方式,使两个模块合作。其次,如果可能,应避免使用字符串访问变量名。特别是在这种情况下,这不是处理进口应该如何工作。话虽这么说,gatattr(或对象.__ dict __ [])就像使用的问题应该工作得很好。