问题:来自对象字段的Python字典
您是否知道是否有内置函数可以从任意对象构建字典?我想做这样的事情:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> props(f)
{ 'bar' : 'hello', 'baz' : 'world' }
注意:它不应包含方法。仅字段。
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I’d like to do something like this:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> props(f)
{ 'bar' : 'hello', 'baz' : 'world' }
NOTE: It should not include methods. Only fields.
回答 0
请注意,Python 2.7中的最佳实践是使用新型类(Python 3不需要),即
class Foo(object):
...
同样,“对象”和“类”之间也存在差异。要从任意对象构建字典,只需使用即可__dict__
。通常,您将在类级别声明您的方法,并在实例级别声明您的属性,因此__dict__
应该没问题。例如:
>>> class A(object):
... def __init__(self):
... self.b = 1
... self.c = 2
... def do_nothing(self):
... pass
...
>>> a = A()
>>> a.__dict__
{'c': 2, 'b': 1}
更好的方法(由robert建议在注释中使用)是内置vars
函数:
>>> vars(a)
{'c': 2, 'b': 1}
另外,根据您要执行的操作,最好继承自dict
。然后,您的Class已经是字典,并且如果您愿意,可以覆盖getattr
和/或setattr
调用并设置字典。例如:
class Foo(dict):
def __init__(self):
pass
def __getattr__(self, attr):
return self[attr]
# etc...
Note that best practice in Python 2.7 is to use new-style classes (not needed with Python 3), i.e.
class Foo(object):
...
Also, there’s a difference between an ‘object’ and a ‘class’. To build a dictionary from an arbitrary object, it’s sufficient to use __dict__
. Usually, you’ll declare your methods at class level and your attributes at instance level, so __dict__
should be fine. For example:
>>> class A(object):
... def __init__(self):
... self.b = 1
... self.c = 2
... def do_nothing(self):
... pass
...
>>> a = A()
>>> a.__dict__
{'c': 2, 'b': 1}
A better approach (suggested by robert in comments) is the builtin vars
function:
>>> vars(a)
{'c': 2, 'b': 1}
Alternatively, depending on what you want to do, it might be nice to inherit from dict
. Then your class is already a dictionary, and if you want you can override getattr
and/or setattr
to call through and set the dict. For example:
class Foo(dict):
def __init__(self):
pass
def __getattr__(self, attr):
return self[attr]
# etc...
回答 1
取而代之的是x.__dict__
,它实际上更具有Pythonic的用法vars(x)
。
Instead of x.__dict__
, it’s actually more pythonic to use vars(x)
.
回答 2
该dir
内置会给你对象的所有属性,包括特殊的方法,如__str__
,__dict__
和一大堆人,你可能不希望的。但是您可以执行以下操作:
>>> class Foo(object):
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> [name for name in dir(f) if not name.startswith('__')]
[ 'bar', 'baz' ]
>>> dict((name, getattr(f, name)) for name in dir(f) if not name.startswith('__'))
{ 'bar': 'hello', 'baz': 'world' }
因此可以通过定义如下props
函数将其扩展为仅返回数据属性而不是方法:
import inspect
def props(obj):
pr = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswith('__') and not inspect.ismethod(value):
pr[name] = value
return pr
The dir
builtin will give you all the object’s attributes, including special methods like __str__
, __dict__
and a whole bunch of others which you probably don’t want. But you can do something like:
>>> class Foo(object):
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> [name for name in dir(f) if not name.startswith('__')]
[ 'bar', 'baz' ]
>>> dict((name, getattr(f, name)) for name in dir(f) if not name.startswith('__'))
{ 'bar': 'hello', 'baz': 'world' }
So can extend this to only return data attributes and not methods, by defining your props
function like this:
import inspect
def props(obj):
pr = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswith('__') and not inspect.ismethod(value):
pr[name] = value
return pr
回答 3
我已经结合了两个答案:
dict((key, value) for key, value in f.__dict__.iteritems()
if not callable(value) and not key.startswith('__'))
I’ve settled with a combination of both answers:
dict((key, value) for key, value in f.__dict__.iteritems()
if not callable(value) and not key.startswith('__'))
回答 4
我以为我会花些时间向您展示如何通过转换对象来决定字典dict(obj)
。
class A(object):
d = '4'
e = '5'
f = '6'
def __init__(self):
self.a = '1'
self.b = '2'
self.c = '3'
def __iter__(self):
# first start by grabbing the Class items
iters = dict((x,y) for x,y in A.__dict__.items() if x[:2] != '__')
# then update the class items with the instance items
iters.update(self.__dict__)
# now 'yield' through the items
for x,y in iters.items():
yield x,y
a = A()
print(dict(a))
# prints "{'a': '1', 'c': '3', 'b': '2', 'e': '5', 'd': '4', 'f': '6'}"
此代码的关键部分是 __iter__
功能。
正如评论所解释的,我们要做的第一件事是获取Class项,并防止以’__’开头的任何东西。
一旦创建了它dict
,就可以使用update
dict函数并传入实例__dict__
。
这些将为您提供完整的成员类+实例字典。现在剩下的就是迭代它们并产生回报。
另外,如果您打算大量使用它,则可以创建一个@iterable
类装饰器。
def iterable(cls):
def iterfn(self):
iters = dict((x,y) for x,y in cls.__dict__.items() if x[:2] != '__')
iters.update(self.__dict__)
for x,y in iters.items():
yield x,y
cls.__iter__ = iterfn
return cls
@iterable
class B(object):
d = 'd'
e = 'e'
f = 'f'
def __init__(self):
self.a = 'a'
self.b = 'b'
self.c = 'c'
b = B()
print(dict(b))
I thought I’d take some time to show you how you can translate an object to dict via dict(obj)
.
class A(object):
d = '4'
e = '5'
f = '6'
def __init__(self):
self.a = '1'
self.b = '2'
self.c = '3'
def __iter__(self):
# first start by grabbing the Class items
iters = dict((x,y) for x,y in A.__dict__.items() if x[:2] != '__')
# then update the class items with the instance items
iters.update(self.__dict__)
# now 'yield' through the items
for x,y in iters.items():
yield x,y
a = A()
print(dict(a))
# prints "{'a': '1', 'c': '3', 'b': '2', 'e': '5', 'd': '4', 'f': '6'}"
The key section of this code is the __iter__
function.
As the comments explain, the first thing we do is grab the Class items and prevent anything that starts with ‘__’.
Once you’ve created that dict
, then you can use the update
dict function and pass in the instance __dict__
.
These will give you a complete class+instance dictionary of members. Now all that’s left is to iterate over them and yield the returns.
Also, if you plan on using this a lot, you can create an @iterable
class decorator.
def iterable(cls):
def iterfn(self):
iters = dict((x,y) for x,y in cls.__dict__.items() if x[:2] != '__')
iters.update(self.__dict__)
for x,y in iters.items():
yield x,y
cls.__iter__ = iterfn
return cls
@iterable
class B(object):
d = 'd'
e = 'e'
f = 'f'
def __init__(self):
self.a = 'a'
self.b = 'b'
self.c = 'c'
b = B()
print(dict(b))
回答 5
要从任意对象构建字典,只需使用即可__dict__
。
这会错过对象从其类继承的属性。例如,
class c(object):
x = 3
a = c()
hasattr(a,’x’)是true,但是’x’不会出现在a .__ dict__
To build a dictionary from an arbitrary object, it’s sufficient to use __dict__
.
This misses attributes that the object inherits from its class. For example,
class c(object):
x = 3
a = c()
hasattr(a, ‘x’) is true, but ‘x’ does not appear in a.__dict__
回答 6
答案较晚,但提供了完整性和对Google员工的好处:
def props(x):
return dict((key, getattr(x, key)) for key in dir(x) if key not in dir(x.__class__))
这不会显示在类中定义的方法,但仍会显示字段,包括分配给lambda的字段或以双下划线开头的字段。
Late answer but provided for completeness and the benefit of googlers:
def props(x):
return dict((key, getattr(x, key)) for key in dir(x) if key not in dir(x.__class__))
This will not show methods defined in the class, but it will still show fields including those assigned to lambdas or those which start with a double underscore.
回答 7
我认为最简单的方法是为该类创建一个getitem属性。如果需要写入对象,则可以创建一个自定义setattr。这是getitem的示例:
class A(object):
def __init__(self):
self.b = 1
self.c = 2
def __getitem__(self, item):
return self.__dict__[item]
# Usage:
a = A()
a.__getitem__('b') # Outputs 1
a.__dict__ # Outputs {'c': 2, 'b': 1}
vars(a) # Outputs {'c': 2, 'b': 1}
dict将对象属性生成到字典中,并且字典对象可用于获取所需的项目。
I think the easiest way is to create a getitem attribute for the class. If you need to write to the object, you can create a custom setattr . Here is an example for getitem:
class A(object):
def __init__(self):
self.b = 1
self.c = 2
def __getitem__(self, item):
return self.__dict__[item]
# Usage:
a = A()
a.__getitem__('b') # Outputs 1
a.__dict__ # Outputs {'c': 2, 'b': 1}
vars(a) # Outputs {'c': 2, 'b': 1}
dict generates the objects attributes into a dictionary and the dictionary object can be used to get the item you need.
回答 8
使用的缺点 __dict__
是它很浅。它不会将任何子类转换为字典。
如果您使用的是Python3.5或更高版本,则可以使用jsons
:
>>> import jsons
>>> jsons.dump(f)
{'bar': 'hello', 'baz': 'world'}
A downside of using __dict__
is that it is shallow; it won’t convert any subclasses to dictionaries.
If you’re using Python3.5 or higher, you can use jsons
:
>>> import jsons
>>> jsons.dump(f)
{'bar': 'hello', 'baz': 'world'}
回答 9
如果要列出部分属性,请覆盖__dict__
:
def __dict__(self):
d = {
'attr_1' : self.attr_1,
...
}
return d
# Call __dict__
d = instance.__dict__()
如果您instance
获得了一些大块数据,并且想要d
像消息队列一样推送到Redis ,这将很有帮助。
If you want to list part of your attributes, override __dict__
:
def __dict__(self):
d = {
'attr_1' : self.attr_1,
...
}
return d
# Call __dict__
d = instance.__dict__()
This helps a lot if your instance
get some large block data and you want to push d
to Redis like message queue.
回答 10
PYTHON 3:
class DateTimeDecoder(json.JSONDecoder):
def __init__(self, *args, **kargs):
JSONDecoder.__init__(self, object_hook=self.dict_to_object,
*args, **kargs)
def dict_to_object(self, d):
if '__type__' not in d:
return d
type = d.pop('__type__')
try:
dateobj = datetime(**d)
return dateobj
except:
d['__type__'] = type
return d
def json_default_format(value):
try:
if isinstance(value, datetime):
return {
'__type__': 'datetime',
'year': value.year,
'month': value.month,
'day': value.day,
'hour': value.hour,
'minute': value.minute,
'second': value.second,
'microsecond': value.microsecond,
}
if isinstance(value, decimal.Decimal):
return float(value)
if isinstance(value, Enum):
return value.name
else:
return vars(value)
except Exception as e:
raise ValueError
现在,您可以在自己的类中使用上述代码:
class Foo():
def toJSON(self):
return json.loads(
json.dumps(self, sort_keys=True, indent=4, separators=(',', ': '), default=json_default_format), cls=DateTimeDecoder)
Foo().toJSON()
PYTHON 3:
class DateTimeDecoder(json.JSONDecoder):
def __init__(self, *args, **kargs):
JSONDecoder.__init__(self, object_hook=self.dict_to_object,
*args, **kargs)
def dict_to_object(self, d):
if '__type__' not in d:
return d
type = d.pop('__type__')
try:
dateobj = datetime(**d)
return dateobj
except:
d['__type__'] = type
return d
def json_default_format(value):
try:
if isinstance(value, datetime):
return {
'__type__': 'datetime',
'year': value.year,
'month': value.month,
'day': value.day,
'hour': value.hour,
'minute': value.minute,
'second': value.second,
'microsecond': value.microsecond,
}
if isinstance(value, decimal.Decimal):
return float(value)
if isinstance(value, Enum):
return value.name
else:
return vars(value)
except Exception as e:
raise ValueError
Now you can use above code inside your own class :
class Foo():
def toJSON(self):
return json.loads(
json.dumps(self, sort_keys=True, indent=4, separators=(',', ': '), default=json_default_format), cls=DateTimeDecoder)
Foo().toJSON()
回答 11
vars()
很棒,但是不适用于对象的嵌套对象
将对象的嵌套对象转换为dict:
def to_dict(self):
return json.loads(json.dumps(self, default=lambda o: o.__dict__))
vars()
is great, but doesn’t work for nested objects of objects
Convert nested object of objects to dict:
def to_dict(self):
return json.loads(json.dumps(self, default=lambda o: o.__dict__))
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。