在python中设置字典中的属性
是否可以在python中从字典创建一个对象,使每个键都是该对象的属性?
像这样的东西:
d = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
e = Employee(d)
print e.name # Oscar
print e.age + 10 # 42
我认为这几乎与这个问题相反:来自对象字段的Python字典
7个解决方案
124 votes
当然,这样的事情:
class Employee(object):
def __init__(self, initial_data):
for key in initial_data:
setattr(self, key, initial_data[key])
更新
正如Brent Nash建议的那样,您也可以通过允许关键字参数来使其更灵活:
class Employee(object):
def __init__(self, *initial_data, **kwargs):
for dictionary in initial_data:
for key in dictionary:
setattr(self, key, dictionary[key])
for key in kwargs:
setattr(self, key, kwargs[key])
然后你可以像这样调用它:
e = Employee({"name": "abc", "age": 32})
或者像这样:
e = Employee(name="abc", age=32)
或者甚至喜欢这样:
employee_template = {"role": "minion"}
e = Employee(employee_template, name="abc", age=32)
Ian Clelland answered 2019-04-26T17:51:53Z
35 votes
以这种方式设置属性几乎肯定不是解决问题的最佳方法。或者:
你知道所有领域应该提前做什么。 在这种情况下,您可以显式设置所有属性。 这看起来像
class Employee(object):
def __init__(self, name, last_name, age):
self.name = name
self.last_name = last_name
self.age = age
d = {'name': 'Oscar', 'last_name': 'Reyes', 'age':32 }
e = Employee(**d)
print e.name # Oscar
print e.age + 10 # 42
要么
你不知道所有领域应该提前做什么。 在这种情况下,您应该将数据存储为dict而不是污染对象命名空间。 属性用于静态访问。 这种情况看起来像
collections.namedtuple
另一个基本上与案例1相同的解决方案是使用collections.namedtuple.请参阅van的答案,了解如何实现这一点。
Mike Graham answered 2019-04-26T17:52:52Z
12 votes
您可以使用__dict__访问对象的属性,并在其上调用update方法:
>>> class Employee(object):
... def __init__(self, _dict):
... self.__dict__.update(_dict)
...
>>> dict = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
>>> e = Employee(dict)
>>> e.name
'Oscar'
>>> e.age
32
Dave Kirby answered 2019-04-26T17:53:17Z
6 votes
如果你真的需要支持.name,我认为使用_asdict()的答案是可行的。
但是如果_asdict()对象只是一个可以使用点语法(.name)而不是dict语法(['name'])访问的结构,则可以像这样使用namedtuple:
from collections import namedtuple
Employee = namedtuple('Employee', 'name age')
e = Employee('noname01', 6)
print e
#>> Employee(name='noname01', age=6)
# create Employee from dictionary
d = {'name': 'noname02', 'age': 7}
e = Employee(**d)
print e
#>> Employee(name='noname02', age=7)
print e._asdict()
#>> {'age': 7, 'name': 'noname02'}
您可以使用_asdict()方法将所有属性作为字典访问,但以后只能在构造期间添加其他属性。
van answered 2019-04-26T17:53:56Z
6 votes
为什么不直接使用属性名作为字典的键?
class StructMyDict(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError as e:
raise AttributeError(e)
def __setattr__(self, name, value):
self[name] = value
您可以使用命名参数,元组列表或字典进行初始化,或者个别属性分配,例如:
nautical = StructMyDict(left = "Port", right = "Starboard") # named args
nautical2 = StructMyDict({"left":"Port","right":"Starboard"}) # dictionary
nautical3 = StructMyDict([("left","Port"),("right","Starboard")]) # tuples list
nautical4 = StructMyDict() # fields TBD
nautical4.left = "Port"
nautical4.right = "Starboard"
for x in [nautical, nautical2, nautical3, nautical4]:
print "%s %s" % (x.left,x.right)
或者,您可以为未知值返回None,而不是引发属性错误。 (web2py存储类中使用的技巧)
RufusVS answered 2019-04-26T17:54:34Z
5 votes
比如说
class A():
def __init__(self):
self.x=7
self.y=8
self.z="name"
如果要一次设置属性
d = {'x':100,'y':300,'z':"blah"}
a = A()
a.__dict__.update(d)
islam ezzat answered 2019-04-26T17:55:06Z
1 votes
类似于使用dict,你可以像这样使用kwargs:
class Person:
def __init__(self, **kwargs):
self.properties = kwargs
def get_property(self, key):
return self.properties.get(key, None)
def main():
timmy = Person(color = 'red')
print(timmy.get_property('color')) #prints 'red'
Alex Spencer answered 2019-04-26T17:55:31Z