Simple question about accessing instance properties.

L

Lacrima

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:
def __init__(self):
self.name = 'John'
self.email = '(e-mail address removed)'
self.phone = '3453454'
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'?

With regards,
Max.
 
M

MRAB

Lacrima said:
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:
def __init__(self):
self.name = 'John'
self.email = '(e-mail address removed)'
self.phone = '3453454'
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'?
'John'
 
L

Lacrima

Lacrima said:
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:
   def __init__(self):
           self.name = 'John'
           self.email = '(e-mail address removed)'
           self.phone = '3453454'
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.
 

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
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top