initialising a class by name

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jan 15 03:24:09 CET 2009


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



More information about the Python-list mailing list