initialising a class by name

Discussion in 'Python' started by Krishnakant, Jan 14, 2009.

  1. Krishnakant

    Krishnakant Guest

    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.
     
    Krishnakant, Jan 14, 2009
    #1
    1. Advertising

  2. On Wed, 14 Jan 2009 11:16:58 +0530, Krishnakant wrote:

    > 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:

    >>> dispatch_table = {}
    >>> for name in dir():

    .... obj = globals()[name]
    .... if type(obj) == type:
    .... dispatch_table[name] = obj
    ....
    >>> dispatch_table

    {'Ham': <class '__main__.Ham'>, 'Spam': <class '__main__.Spam'>}



    --
    Steven
     
    Steven D'Aprano, Jan 14, 2009
    #2
    1. Advertising

  3. Krishnakant

    Krishnakant Guest

    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.
    On Wed, 2009-01-14 at 07:50 +0000, Steven D'Aprano wrote:
    > On Wed, 14 Jan 2009 11:16:58 +0530, Krishnakant wrote:
    >
    > > 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:
    >
    > >>> dispatch_table = {}
    > >>> for name in dir():

    > ... obj = globals()[name]
    > ... if type(obj) == type:
    > ... dispatch_table[name] = obj
    > ...
    > >>> dispatch_table

    > {'Ham': <class '__main__.Ham'>, 'Spam': <class '__main__.Spam'>}
    >
    >
    >
     
    Krishnakant, Jan 14, 2009
    #3
  4. On Wed, 14 Jan 2009 13:53:52 +0530, Krishnakant wrote:

    > 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.



    --
    Steven
     
    Steven D'Aprano, Jan 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jan

    Initialising a signal

    jan, Dec 16, 2003, in forum: VHDL
    Replies:
    3
    Views:
    497
    David Jones
    Dec 23, 2003
  2. Tim Clacy
    Replies:
    3
    Views:
    592
    Nick Hounsome
    Mar 4, 2004
  3. =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

    Attempt at initialising a class with a vector "inline"

    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Aug 12, 2004, in forum: C++
    Replies:
    6
    Views:
    455
    Rob Williscroft
    Aug 12, 2004
  4. Fred
    Replies:
    6
    Views:
    388
  5. mark4asp
    Replies:
    2
    Views:
    351
    mark4asp
    Jul 5, 2007
Loading...

Share This Page