using variable-value

T

Tor Erik Sønvisen

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?

# 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' "...

regards tores
 
B

bruno modulix

Tor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,005
Latest member
ChasityFan

Latest Threads

Top