Simple question about accessing instance properties.

Lacrima Lacrima.Maxim at gmail.com
Thu May 21 12:14:57 EDT 2009


On May 21, 7:04 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> Lacrima wrote:
> > Hello!
>
> > I think I have a very simple question, but I can't understand how to
> > access object properties in a way described below.
> > For example I have an instance of any class:
>
> >>>> class Person:
> >    def __init__(self):
> >            self.name = 'John'
> >            self.email = 'j... at example.com'
> >            self.phone = '3453454'
> >>>> person = Person()
>
> > Then I have a list of person's properties represented as strings:
> >>>> prop_list = ['name', 'email', 'phone']
>
> > And my question is how to access person's properties using prop_list?
> > Do I have to somehow convert 'name', 'email', 'phone'?
>
>  >>> getattr(person, "name")
> 'John'

Hi!
Thanks a lot!!!
That's so simple!

With regards,
Max.



More information about the Python-list mailing list