問題:
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