编辑:我应该指定我(卡住)使用Python 2,但是有兴趣看看如何在2或3中解决这个问题
情景:
我有一个名为shapes
的包。
我在shapes
中有一个名为factory
的模块,其中有一个ShapeClassFactory
类。
这个类可以传递一个字符串,它将在远程数据库中查找数据并使用它来动态定义它返回的类。
shapes.py:
from .factory import ShapeClassFactory
__all__ = ['ShapeClassFactory']
在实践中,该包装可用于各种其他包装和包装中。脚本,如:
from shapes import ShapeClassFactory
Circle = ShapeClassFactory("Circle")
Rect = ShapeClassFactory("Rect")
myCircle = Circle(r=5, fill='red')
mySquare = Rect(x=5, y=5, fill=None)
问题:
以上都很好。但是,我希望能够以这样的方式编写shapes
包,使其可以像这样使用:
from shapes import Circle, Rect
myCircle = Circle(r=5, fill='red')
mySquare = Rect(x=5, y=5, fill=None)
...我的想法是,如果在shapes
中找不到该成员,则会使用ShapeClassFactory
来尝试生成该成员。
困难在于,在请求之前基本上不知道可用的类,因此预定义的类名列表将无济于事。
如果ImportError
无法建立课程,我不介意抛出ShapeClassFactory
- 但是这样的事情甚至可能吗?
答案 0 :(得分:0)
你可以通过在初始化时自动构建shapes
命名空间中的所有可能对象来做到这一点,只要没有太多可能的类并且预先初始化类的成本不是太高。你可以在shapes.py中使用这样的代码:
from .factory import ShapeClassFactory
__all__ = ['ShapeClassFactory']
def get_shape_names():
"""Returns all valid shapes that can be passed in to ShapeClassFactory"""
return ['Circle', 'Rect'] # your own code should query the database
for name in get_shape_names():
globals()[name] = ShapeClassFactory(name)
__all__.append(name)