问题:
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.只有字段。
解决方案:
参考一: https://stackoom.com/question/G0D参考二: Python dictionary from an object's fields