On Wed, Aug 20, 2008 at 2:30 AM, Jim Morcombe <[EMAIL PROTECTED]>wrote:

> I have an object called "myObject" with an attribute called "colour".
>
> If  I type:
>   print myObject.colour
> then python prints:
>    red
>
> Now, I have a variable "myAttribute" and set its value to "colour" by
> typing:
>   myAttribute = "colour"
>
> I want to have a line of code:  something like this:
>   print myObject.myAttribute
> and have Python interpret it as meaning "print myObject.colour" and hence
> print "red"
>
>
> Can this be done?  If so, how?
>
> Jim Morcombe
>

print(getattr(myObject, "colour"))

Here's the effbot article:  http://effbot.org/zone/python-getattr.htm

-- 
www.fsrtechnologies.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to