Create object from variable indirect reference?

Rami Chowdhury rami.chowdhury at gmail.com
Tue Nov 10 10:13:54 EST 2009


On Tue, 10 Nov 2009 06:59:25 -0800, NickC <reply-to at works.fine.invalid>  
wrote:

> I can't seem to find a way to do something that seems straighforward, so  
> I
> must have a mental block.  I want to reference an object indirectly
> through a variable's value.
>
> Using a library that returns all sorts of information about "something",  
> I
> want to provide the name of the "something" via a variable (command line
> argument).  The something is a class in the library and I want to
> instantiate an object of the class so I can start querying it.  I can't
> figure out how to pass the name of the class to the library.
>
> Or, put another way, I can't figure out how to indirectly reference the
> value of the command line argument to create the object.
>
> To make it clearer, it's roughly equivalent to this in bash:
>  Sun="1AU" ; body=Sun; echo ${!body} --> outputs "1AU".
>
> command line:
> $ ./ephemeris.py Moon
>
> code:
> import ephem
> import optparse
>
> # various option parsing (left out for brevity),
> # so variable options.body contains string "Moon",
> # or even "Moon()" if that would make it easier.
>
> # Want to instantiate an object of class Moon.
> # Direct way:
> moon1 = ephem.Moon()
> # Indirect way from command line with a quasi bashism that obviously  
> fails:
> moon2 = ephem.${!options.body}()
>
> Can someone point me in the right direction here?
>
> (The library is PyEphem, an extraordinarily useful library for anyone
> interested in astronomy.)
>
> Many thanks,
>

Since Python 'variables' are really keys in a namespace dictionary, it's  
fairly straightforward to get at them given a string value -- what you  
probably want in this case is the built-in function getattr()  
(http://www.diveintopython.org/power_of_introspection/getattr.html)...

So getattr(ephem, "Moon") should give you the class object ephem.Moon,  
which you can then instantiate...



-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list