Variable class instantiation

2009-12-11 Thread Jan Mach
Hi everybody, I am currently solving the following problem and I am stuck. I am trying to create instance of the class of variable name. I know, that the following works: if (something): classToUse = C1 else: classToUse = C2 o = classToUse() ,but this is not what I want. I need to have

Re: Variable class instantiation

2009-12-11 Thread Richard Thomas
On Dec 11, 9:26 am, Jan Mach jan.m...@cesnet.cz wrote: Hi everybody, I am currently solving the following problem and I am stuck. I am trying to create instance of the class of variable name. I know, that the following works: if (something):     classToUse = C1 else:     classToUse = C2

Re: Variable class instantiation

2009-12-11 Thread Steven D'Aprano
On Fri, 11 Dec 2009 10:26:49 +0100, Jan Mach wrote: I need to have the class name in the string and then instantiate it: (this does not work of course) classToUse = C1 o = classToUse() It is because I don`t know at the moment what the name of the class will be, I will load it from the

Re: Variable class instantiation

2009-12-11 Thread Lie Ryan
On 12/11/2009 8:26 PM, Jan Mach wrote: Hi everybody, I am currently solving the following problem and I am stuck. I am trying to create instance of the class of variable name. I know, that the following works: if (something): classToUse = C1 else: classToUse = C2 o = classToUse()

Re: Variable class instantiation

2009-12-11 Thread Sion Arrowsmith
Steven D'Aprano st...@remove-this-cybersource.com.au wrote: thisModule = __import__(__name__) classToUse = thisModule.__dict__['C1'] Any reason to prefer this over: classToUse = getattr(thisModule, 'C1') ? (I think, for a module, they should do exactly the same thing. Personally, I prefer

Re: Variable class instantiation

2009-12-11 Thread Steven D'Aprano
On Fri, 11 Dec 2009 11:36:13 +, Sion Arrowsmith wrote: Steven D'Aprano st...@remove-this-cybersource.com.au wrote: thisModule = __import__(__name__) classToUse = thisModule.__dict__['C1'] Any reason to prefer this over: classToUse = getattr(thisModule, 'C1') ? (I think, for a