来自对象字段的 Python 字典 - Python dictionary from an object's fields

问题:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章