Re: initialising a class by name

2009-01-14 Thread Steven D'Aprano
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 {

Re: initialising a class by name

2009-01-14 Thread Nick Craig-Wood
Krishnakant wrote: > 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

Re: initialising a class by name

2009-01-14 Thread Sion Arrowsmith
Krishnakant wrote: >By the way, is there a kind of global list of modules/classes which are >maintained in a package once the program is loaded into memory? sys.modules is a dict of loaded module objects, keyed by module name. So: >>> getattr(sys.modules["sys"], "version_info") (2, 5, 0, 'final

Re: initialising a class by name

2009-01-14 Thread Willi Richert
Hi, try the following exemplarily for the os module import os, types [(c, klass) for (c,klass) in os.__dict__.items() if type(klass)==types.ClassType] will print: [('_Environ', )] Regards, wr Am Mittwoch, 14. Januar 2009 10:55:27 schrieb Krishnakant: > On Wed, 2009-01-14 at 00:39 -0800, Chris

Re: initialising a class by name

2009-01-14 Thread Krishnakant
On Wed, 2009-01-14 at 00:39 -0800, Chris Rebert wrote: > On Wed, Jan 14, 2009 at 12:36 AM, Krishnakant wrote: > > On Wed, 2009-01-14 at 00:20 -0800, Chris Rebert wrote: > >> Aside from Steven's excellent idea, to use the getattr() technique > >> with your module scheme you'd probably also need to

Re: initialising a class by name

2009-01-14 Thread Krishnakant
On Wed, 2009-01-14 at 04:09 -0500, Steve Holden wrote: > > > You don't need to have the names of the classes related to anything in > the interface. Just use a list of classes, and have the user interface > return the correct index for each class. Then (supposing the selection > by the user is sel

Re: initialising a class by name

2009-01-14 Thread Steve Holden
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. > Is this possible in python and how? > To make things clear, let me give the real example. > there is an inventory

Re: initialising a class by name

2009-01-14 Thread Chris Rebert
On Wed, Jan 14, 2009 at 12:36 AM, Krishnakant wrote: > On Wed, 2009-01-14 at 00:20 -0800, Chris Rebert wrote: >> Aside from Steven's excellent idea, to use the getattr() technique >> with your module scheme you'd probably also need to use __import__() >> to dynamically import the right module. >>

Re: initialising a class by name

2009-01-14 Thread Krishnakant
On Wed, 2009-01-14 at 00:20 -0800, Chris Rebert wrote: > Aside from Steven's excellent idea, to use the getattr() technique > with your module scheme you'd probably also need to use __import__() > to dynamically import the right module. > I would generally import all the modules I would need at th

Re: initialising a class by name

2009-01-14 Thread Steven D'Aprano
On Wed, 14 Jan 2009 13:19:23 +0530, Krishnakant wrote: > On Tue, 2009-01-13 at 21:51 -0800, Chris Rebert wrote: >> Assuming all the classes are in the same module as the main program: >> >> instance = vars()[class_name](args, to, init) >> > The classes are not in the same module. Every glade win

Re: initialising a class by name

2009-01-14 Thread 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 wi

Re: initialising a class by name

2009-01-14 Thread Chris Rebert
On Wed, Jan 14, 2009 at 12:15 AM, Krishnakant wrote: > Hi, > So should I not use getattr()? > If I have one class in one module, then should I use global? > I found getattr() very easy to use, my only dowbt is that if there is > going to be one class per module then will it be a good idea? > some

Re: initialising a class by name

2009-01-14 Thread Krishnakant
Hi, So should I not use getattr()? If I have one class in one module, then should I use global? I found getattr() very easy to use, my only dowbt is that if there is going to be one class per module then will it be a good idea? some thing like module, class_name happy hacking. Krishnakantt. On Tu

Re: initialising a class by name

2009-01-13 Thread Chris Rebert
On Tue, Jan 13, 2009 at 11:49 PM, Krishnakant wrote: > On Tue, 2009-01-13 at 21:51 -0800, Chris Rebert wrote: >> Assuming all the classes are in the same module as the main program: >> >> instance = vars()[class_name](args, to, init) >> > The classes are not in the same module. > Every glade windo

Re: initialising a class by name

2009-01-13 Thread Steven D'Aprano
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 u

Re: initialising a class by name

2009-01-13 Thread Krishnakant
On Tue, 2009-01-13 at 21:51 -0800, Chris Rebert wrote: > Assuming all the classes are in the same module as the main program: > > instance = vars()[class_name](args, to, init) > The classes are not in the same module. Every glade window is coupled with one py file (module) containing one class th

Re: initialising a class by name

2009-01-13 Thread Chris Rebert
On Tue, Jan 13, 2009 at 9:46 PM, 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. > Is this possible in python and how? > To make things clear, let me give the rea

initialising a class by name

2009-01-13 Thread 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 b