简体   繁体   中英

How to change module of a class in Python?

module1 have funcion make_class. module2 calls it this way:

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'> ? (I need this to fulfill the requirements of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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