Suppose I have the code below:
class Person(object):
""" Capture user demographics data"""
def __init__(self, name, address, phone, gender, name_prefix):
""" Initialize the object """
self.name = name
self.address = address
self.phone = phone
self.gender = gender
self.prefix = name_prefix
def display_userdata(self):
""" Returns user data"""
userdata = {'name':self.name, 'address': self.address,
'phone': self.phone, 'gender': self.gender, 'prefix': self.prefix
}
return userdata
I can initialize the data:
newperson = Person("Ben", '9999 Gotham City, las vegas', '702-000-0000', 'male', 'Waiter')
But I have a feeling that the display_userdata() function is redundant if I could re-write __init to store as dict.
newperson.display_userdata()
It returns the output:
{'address': '9999 Gotham City, las vegas',
'gender': 'male',
'name': 'Ben',
'phone': '702-000-0000',
'prefix': 'Waiter'}
My questions are:
Is there a smarter way to write the __init__ snippet so the input is stored directly as python dictionary? I don't want to call the constructor with dict key by using setattr.
Secondly, Suppose the user has 3 phones or more (variable), how do I store this in an array while calling the object constructor. Think self.phone = ['702-000-000', '413-222-3333' ]