您知道是否有一个内置函数可以从任意对象构建字典吗?我想这样做:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> props(f)
{ 'bar' : 'hello', 'baz' : 'world' }
注意:它不应该包括方法。只有字段。
您知道是否有一个内置函数可以从任意对象构建字典吗?我想这样做:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
>>> f = Foo()
>>> props(f)
{ 'bar' : 'hello', 'baz' : 'world' }
注意:它不应该包括方法。只有字段。
当前回答
使用vars(x)而不是x.__dict__实际上更python化。
其他回答
正如上面的一条评论中提到的,vars目前不是通用的,因为它不适用于具有__slots__而不是普通__dict__的对象。此外,一些对象(例如,str或int等内置对象)既没有__dict__也没有__slots__。
目前,一个更通用的解决方案可能是:
def instance_attributes(obj: Any) -> Dict[str, Any]:
"""Get a name-to-value dictionary of instance attributes of an arbitrary object."""
try:
return vars(obj)
except TypeError:
pass
# object doesn't have __dict__, try with __slots__
try:
slots = obj.__slots__
except AttributeError:
# doesn't have __dict__ nor __slots__, probably a builtin like str or int
return {}
# collect all slots attributes (some might not be present)
attrs = {}
for name in slots:
try:
attrs[name] = getattr(obj, name)
except AttributeError:
continue
return attrs
例子:
class Foo:
class_var = "spam"
class Bar:
class_var = "eggs"
__slots__ = ["a", "b"]
>>> foo = Foo()
>>> foo.a = 1
>>> foo.b = 2
>>> instance_attributes(foo)
{'a': 1, 'b': 2}
>>> bar = Bar()
>>> bar.a = 3
>>> instance_attributes(bar)
{'a': 3}
>>> instance_attributes("baz")
{}
咆哮:
遗憾的是,这还没有内置到vars中。Python中的许多内建承诺是问题的“解决方案”,但总有一些特殊情况没有得到处理……在任何情况下,最终都必须手动编写代码。
Python3.x
return dict((key, value) for key, value in f.__dict__.items() if not callable(value) and not key.startswith('__'))
Try:
from pprint import pformat
a_dict = eval(pformat(an_obj))
我认为最简单的方法是为类创建一个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将对象属性生成到字典中,可以使用字典对象获取所需的项。
如果你想列出部分属性,重写__dict__:
def __dict__(self):
d = {
'attr_1' : self.attr_1,
...
}
return d
# Call __dict__
d = instance.__dict__()
如果你的实例得到一些大的块数据,并且你想把d推到Redis消息队列中,这很有帮助。