turning a string into an object name

A. Jones netzapper at magicstar.net
Thu Apr 4 01:53:54 EST 2002


On 4 Apr 2002 00:58:10 GMT, Marco Herrn <herrn at gmx.net> wrote:

>Hello. I know that it is possible to turn a string into a funtion call by
>using getattr(). But what I want to achieve is turning a string into an
>object name. 
>I would have a method like
>
>  foo(self, objname, objtype)
>
>Then I would call this method like
>  
>  foo(myObj, bar)
>
>Then the method foo should create an object with the name "myObj" from the
>type bar.
>
>Is such an thing possible? And if, how?

I'll probably get blasted for this by Python gurus... but, I've used
it with some success.  You can do

exec(myObjc + " = " + value)

or, if it's a class instance you want to create

exec(myObjc + " = " + bar + "()").

Toss whatever variables you want sent on to the instance inside "("
and ")" and you're homefree.

Unless there's some problem with that that an old-timer spots that I
haven't yet.

Netzapper/Aubrey Jones


-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



More information about the Python-list mailing list