首页 > 资讯 > 正文

Python如何使用vars返回对象的属性列表

2020-10-17

英文文档:

vars([object])

Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute.
Objects such as modules and instances have an updateable __dict__ attribute; however, other objects may have write restrictions on their __dict__ attributes (for example, classes use a dictproxy to prevent direct dictionary updates).
Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.

  返回当前作用域内的局部变量和其值组成的字典,或者返回对象的属性列表

说明

  1. 当函数不接收参数时,其功能和locals函数一样,返回当前作用域内的局部变量。

  2. 当函数接收一个参数时,参数可以是模块、类、类实例,或者定义了__dict__属性的对象。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。