使用数组数据

时间:2015-07-14 01:42:55

标签: python oop python-3.x

假设我有以下代码:

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

我可以初始化数据:

newperson = Person("Ben", '9999 Gotham City, las vegas', '702-000-0000', 'male', 'Waiter')

但是如果我可以重新编写display_userdata()来存储为__init,我感觉dict函数是多余的。

newperson.display_userdata()

返回输出:

{'address': '9999 Gotham City, las vegas',
 'gender': 'male',
 'name': 'Ben',
 'phone': '702-000-0000',
 'prefix': 'Waiter'}

我的问题是:

是否有更聪明的方法来编写__init__代码段,以便输入直接存储为python字典?我不想使用setattr来调用带有dict键的构造函数。

其次,假设用户有3部电话或更多(变量),如何在调用对象构造函数时将其存储在数组中。想想self.phone = ['702-000-000', '413-222-3333' ]

3 个答案:

答案 0 :(得分:2)

为什么不直接在init中创建dict?

class Person(object):
""" Capture user demographics data"""

    def __init__(self, name, address, phone, gender, prefix):
        """ Initialize the object """
        self.userdata = {'name': name, 'address': address, 
                'phone': phone, 'gender': gender, 'prefix': prefix
               }

然后

newperson = Person(name="Ben", address='9999 Gotham City, las vegas', phone='702-000-0000', gender ='male', prefix ='Waiter')
print newperson.userdata

返回

{'phone': '702-000-0000', 'gender': 'male', 'prefix': 'Waiter', 'name': 'Ben', 'address': '9999 Gotham City, las vegas'}

对于第二个问题,如果您将列表而不是字符串传递给将显示为列表的phone参数,那么它会起作用吗?

newperson = Person(name="Ben", address='9999 Gotham City, las vegas', phone=['702-000-0000', '111-827-3509'], gender ='male', prefix ='Waiter')
print newperson.userdata

返回

{'phone': ['702-000-0000', '111-827-3509'], 'gender': 'male', 'prefix': 'Waiter', 'name': 'Ben', 'address': '9999 Gotham City, las vegas'}

答案 1 :(得分:1)

你可以通过在python(** kwargs)中使用关键字参数来实现它。 https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments

答案 2 :(得分:1)

如果您的数据为list,则在创建Person实例时,您希望将其作为参数传递,原因如下:

mydata = ["Ben", '9999 Gotham City, las vegas', '702-000-0000', 'male', 'Waiter']

class Person(object):
    """ Capture user demographics data"""
    def __init__(self, *data):
        self.userdata = {'name': data[0], 'address': data[1],
                'phone': data[2], 'gender': data[3], 'prefix': data[4]
               }
    def test(self):
            print self.userdata

>>>newPerson = Person(*mydata)
>>>newPerson.test()
{'phone': '702-000-0000', 'gender': 'male', 'prefix': 'Waiter', 'name': 'Ben', 'address': '9999 Gotham City, las vegas'}

但是,您必须以标准方式将您的数据整理到list, 否则,您最好使用dictionary代替list,这样:

>>>mydata = {'name':"Ben", 'address':'9999 Gotham City, las vegas', 'phone':'702-000-0000', 'gender':'male', 'prefix':'Waiter'}

>>>class Person(object):
       """ Capture user demographics data"""

       def __init__(self, **kwargs):
            """ Initialize the object """
           self.userdata = kwargs
       def test(self):
            print self.userdata

>>>newPerson = Person(**mydata)
>>>newPerson.test()
{'phone': '702-000-0000', 'gender': 'male', 'prefix': 'Waiter', 'name': 'Ben', 'address': '9999 Gotham City, las vegas'}