using variable-value

bruno modulix onurb at xiludom.gro
Wed Sep 21 11:04:05 EDT 2005


Tor Erik Sønvisen wrote:
> Hi
> 
> In php I can assign a value to a variable and use this varaible to access a 
> property in some object:
> 
> $var = 'property';
> $object->{$var}
> 
> This will transelate to $object->property...
> Is this possible in Python?

Not directly, but there's a way: getattr(obj, attname, [,default])

> # Prints help on methods in Canvas-instance
> for method in dir(self.canvas):
>     print method
>     print help(self.canvas.method)
> 
> gives me " AttributeError: Canvas instance has no attribute 'method' "...

Try this
 for method in dir(self.canvas):
     print method
     print help(getattr(self.canvas, "method"))

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list