Using exec
Roman Suzi
rnd at onego.ru
Wed Aug 29 09:09:35 EDT 2001
On Wed, 29 Aug 2001, Kerim Borchaev wrote:
> I need to reference an object's method given a string representation
> of it's name. Now I do it like this:
>
> exec( "c."+MethonName+"()" )
getattr(c, "MethonName")()
> or if I need to create an instance of class named ClassName I do this:
>
> exec( "inst="+ClassName+"()" )
inst = eval(ClassName)()
or even
inst = vars()[ClassName]()
(or globals() instead of vars())
> but it seems ugly to me.
Depends on what are you trying to do with these things.
> Is it the right thing to do it this way?
>
> Thanks in advance,
> Kerim mailto:warkid at storm.ru
There is also Russian Python related maillist at python at list.glasnet.ru
You are welcome!
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
More information about the Python-list
mailing list