mercredi 21 août 2019

create instance by full name in Python

I had 2 class with the same name in different folders. I want to get instance by their name. How can I do? Folders as:

folderA/User.py  
folderB/User.py

Both User.py define a class named User. Firstly, I want import both class User in one file, say mymain.py . Secondly, I want create 2 instances by name string. I tried:

  1. in both folderA/__init__.py add line:

    from . import User

  2. in mymain.py add line:

    import User
    construct1 = globals("folderA.User.User")

    construct2 = globals("folderB.User.User")

won't work.Is there any way for me?





Aucun commentaire:

Enregistrer un commentaire