简体   繁体   中英

Import a list of classes from a module

Looking to import a list of classes and then use them later in the script. Therefore, the from x import * logic does not work. Here is a more specific layout of what I am looking to do.

class_list = [x, y, z, zz, zzz]
from my_module import class_list

and then later on in the code still be able to call x.random_attribute . Not sure if this is possible!

To clarify I am trying to avoid the following:

from my_module import x, y, z, zz, zzz

as I have about 50 class objects I am importing and more will be added over time. Would love to have the list as a separate object.

class_list = ['x', 'y', 'z', 'zz', 'zzz']
for c in class_list:
    exec('from my_module import ' + c)

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