您知道是否有一个内置函数可以从任意对象构建字典吗?我想这样做:
>>> 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' }
注意:它不应该包括方法。只有字段。
当前回答
注意,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继承可能会更好。那么你的类已经是一个字典,如果你愿意,你可以重写getattr和/或setattr来调用和设置字典。例如:
class Foo(dict):
def __init__(self):
pass
def __getattr__(self, attr):
return self[attr]
# etc...
其他回答
我给出了两个答案的组合:
dict((key, value) for key, value in f.__dict__.iteritems()
if not callable(value) and not key.startswith('__'))
在2021年,对于嵌套对象/dicts/json使用pydantic BaseModel -将嵌套dicts和嵌套json对象转换为python对象和json,反之亦然:
https://pydantic-docs.helpmanual.io/usage/models/
>>> class Foo(BaseModel):
... count: int
... size: float = None
...
>>>
>>> class Bar(BaseModel):
... apple = 'x'
... banana = 'y'
...
>>>
>>> class Spam(BaseModel):
... foo: Foo
... bars: List[Bar]
...
>>>
>>> m = Spam(foo={'count': 4}, bars=[{'apple': 'x1'}, {'apple': 'x2'}])
对象to dict
>>> print(m.dict())
{'foo': {'count': 4, 'size': None}, 'bars': [{'apple': 'x1', 'banana': 'y'}, {'apple': 'x2', 'banana': 'y'}]}
对象转换为JSON
>>> print(m.json())
{"foo": {"count": 4, "size": null}, "bars": [{"apple": "x1", "banana": "y"}, {"apple": "x2", "banana": "y"}]}
反对的词典
>>> spam = Spam.parse_obj({'foo': {'count': 4, 'size': None}, 'bars': [{'apple': 'x1', 'banana': 'y'}, {'apple': 'x2', 'banana': 'y2'}]})
>>> spam
Spam(foo=Foo(count=4, size=None), bars=[Bar(apple='x1', banana='y'), Bar(apple='x2', banana='y2')])
JSON到对象
>>> spam = Spam.parse_raw('{"foo": {"count": 4, "size": null}, "bars": [{"apple": "x1", "banana": "y"}, {"apple": "x2", "banana": "y"}]}')
>>> spam
Spam(foo=Foo(count=4, size=None), bars=[Bar(apple='x1', banana='y'), Bar(apple='x2', banana='y')])
要从任意对象构建字典,使用__dict__就足够了。
这将遗漏对象从其类继承的属性。例如,
class c(object):
x = 3
a = c()
Hasattr (a, 'x')为真,但'x'没有出现在a.__dict__中
正如上面的一条评论中提到的,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中的许多内建承诺是问题的“解决方案”,但总有一些特殊情况没有得到处理……在任何情况下,最终都必须手动编写代码。
Try:
from pprint import pformat
a_dict = eval(pformat(an_obj))