[英]How to change module of a class in Python?
module1 have funcion make_class. module1具有funcion make_class。 module2 calls it this way:
module2这样调用:
Foo = make_class('Foo')
or this: 或这个:
setattr(sys.modules[__name__], 'Foo', make_class('Foo'))
But in either case we have <class 'module1.Foo'>
how to assign this class to module2: <class 'module2.Foo'>
? 但是无论哪种情况,我们都有
<class 'module1.Foo'>
如何将此类分配给module2: <class 'module2.Foo'>
吗? (I need this to fulfill the requirements of SQLAlchemy) (我需要这个来满足SQLAlchemy的要求)
Foo.__module__ = __name__
Change the module name explicitly. 显式更改模块名称。 You can have the
make_class
function handle this if you have it take a module name argument for the new class's __module__
attribute. 如果让新类的
__module__
属性带有模块名称参数,则可以使make_class
函数处理此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.