Instantiating variable classes

Wouter van Marle wouterm at spammers-unite-here.com
Thu Mar 20 10:21:33 CET 2003


Harald,

I am thinking of something like this (don't have a python interpreter at
hand to try):

Instances = [repcls0(), repcls1(), repcls2()]
myClass = Instances[X]

Should do the job. You basically stuff all your functions in an array and
call the right one directly. It is one of the things I love about python -
you can stuff dicts in an array, arrays in a dict, functions in an array,
etc. Anything is allowed.

Wouter.

"Harald Massa" <cpl.19.ghum at spamgourmet.com> wrote in message
news:Xns93445C6F5D567cpl19ghumspamgourmet at 62.153.159.134...
> Hello,
>
> I need to instantiate "variable classes". That is:
>
>
> myClass=replcsX()
>
> where X is 0,1,2,3,4,...7
>
> depending on the program flow.
>
> One possibility is:
>
> if X==0:
>     myClass=replcls0()
> elif x==1:
>     myClass=replcls1()
> and so on ..
>
>
> Second possibility is:
>
> command="replcs%d()" % (X,)
> myClass=eval(command)
>
>
> I'm using the second one, but I am sure there is a way to use Python's
> more than powerfull class- and objekt manipulations.
>
> But I did not find out how.
>
> Any suggestions?
>
> Harald
>
>
>
>
>






More information about the Python-list mailing list