initialising a class by name

K

Krishnakant

hello all,
I have a strange situation where I have to load initiate an instance of
a class at run-time with the name given by the user from a dropdown
list.
Is this possible in python and how?
To make things clear, let me give the real example.
there is an inventory management system and products belong to different
categories.
There are predefined categories in the database and for each category
there is a module which contains a class made out of pygtk.
This means what class gets instantiated and displayed in the gui depends
on the choice a user makes in the dropdown.
Now, I could have created a list of if conditions for all the categories
as in
if categorySelection == "books":
Books = BookForm()

However this is a problem because when there will be more than 100
categories there will be that many if conditions and this will make the
code uggly.
so my idea is to name the class exactly after the name of the category
so that when the user selects a category that name is used to initialise
the instance of that class.
So is it possible to initialise an instance of a class given its name
from a variable?
thanks and
Happy hacking.
Krishnakant.
 
S

Steven D'Aprano

hello all,
I have a strange situation where I have to load initiate an instance of
a class at run-time with the name given by the user from a dropdown
list.

Not strange at all.
Is this possible in python and how?

Of course. Just use a dispatch table. Use a dict to map user strings to
classes:

class Spam(object): pass ....
class Ham(object): pass ....
dispatch_table = {"Spam": Spam, "Ham": Ham}
dispatch_table["Ham"]()
<__main__.Ham object at 0xb7ea2f8c>


The keys don't even have to be the name of the class, they can be
whatever input your users can give:
dispatch_table["Yummy meat-like product"] = Spam
dispatch_table["Yummy meat-like product"]()
<__main__.Spam object at 0xb7ea2f6c>


You can even automate it:
.... obj = globals()[name]
.... if type(obj) == type:
.... dispatch_table[name] = obj
....{'Ham': <class '__main__.Ham'>, 'Spam': <class '__main__.Spam'>}
 
K

Krishnakant

Hi steevan,
I liked this idea of dispatchTable.
is it possible to say some thing like
inst = dispatchTable{"ham"}
according to me, inst will become the instance of class ham.
Another thing to note is that all the classes are in different modules.
So where do I create the dict of classes mapped with the name?
happy hacking.
Krishnakant.
hello all,
I have a strange situation where I have to load initiate an instance of
a class at run-time with the name given by the user from a dropdown
list.

Not strange at all.
Is this possible in python and how?

Of course. Just use a dispatch table. Use a dict to map user strings to
classes:

class Spam(object): pass ...
class Ham(object): pass ...
dispatch_table = {"Spam": Spam, "Ham": Ham}
dispatch_table["Ham"]()
<__main__.Ham object at 0xb7ea2f8c>


The keys don't even have to be the name of the class, they can be
whatever input your users can give:
dispatch_table["Yummy meat-like product"] = Spam
dispatch_table["Yummy meat-like product"]()
<__main__.Spam object at 0xb7ea2f6c>


You can even automate it:
... obj = globals()[name]
... if type(obj) == type:
... dispatch_table[name] = obj
...{'Ham': <class '__main__.Ham'>, 'Spam': <class '__main__.Spam'>}
 
S

Steven D'Aprano

Hi steevan,
I liked this idea of dispatchTable.
is it possible to say some thing like inst = dispatchTable{"ham"}
according to me, inst will become the instance of class ham.

Yes, that works, provided you fix the syntax. (You used {} instead of
square brackets.)

There's nothing special about dispatching: you look up the dict with a
key, and that returns a class. You then call that class to create a class
instance as if you were calling it directly by name.

Another
thing to note is that all the classes are in different modules. So where
do I create the dict of classes mapped with the name?

That's up to you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top