Use `__dict__` or `vars()`?

前端 未结 3 1246
忘了有多久
忘了有多久 2020-12-01 07:33

Builtin function vars() looks more Pythonic to me, but I see __dict__ used more frequently.

The Python documentation indicates that they ar

3条回答
  •  离开以前
    2020-12-01 07:53

    Generally, you should consider dunder/magic methods to be the implementation and invoking functions/methods as the API, so it would be preferable to use vars() over __dict__, in the same way that you would do len(a_list) and not a_list.__len__(), or a_dict["key"] rather than a_dict.__getitem__('key')

提交回复
热议问题