博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python vars()_Python vars()
阅读量:2533 次
发布时间:2019-05-11

本文共 3829 字,大约阅读时间需要 12 分钟。

python vars()

Python vars() function returns __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. So the output of vars() function is a .

Python vars()函数为模块,类,实例或具有__dict__属性的任何其他对象返回__dict__属性。 因此,vars()函数的输出是一个 。

Python vars() (Python vars())

Python vars() function syntax is:

Python vars()函数语法为:

vars([object])
  • If no argument is provided, vars() act like function.

    如果未提供任何参数,则vars()的作用类似于函数。
  • The arguments can be module, class or instance of a class.

    参数可以是模块,类或类的实例。
  • If the specified argument doesn’t have __dict__ attribute, the TypeError exception is thrown with message TypeError: vars() argument must have __dict__ attribute.

    如果指定的参数不具有__dict__属性,则会引发TypeError异常并显示消息TypeError: vars() argument must have __dict__ attribute
  • If we update the object __dict__ dictionary values, then the updated value will be returned by vars() function.

    如果我们更新对象__dict__字典值,则更新后的值将由vars()函数返回。

类和对象的vars() (vars() of class and object)

Let’s say we have a class defined with some class variable and instance variables.

假设我们定义了一个包含一些类变量和实例变量的类。

class Data:    # class variables    id = 0    name = ''    def __init__(self, i, n):        self.id = i        self.name = n        # instance variable        self.repr = 'Data[%s,%s]' % (i,n)

Let’s see the vars() function output when an instance of the class is provided.

让我们看一下提供类实例时的vars()函数输出。

d = Data(1, 'Pankaj')# vars of objectprint(vars(d))# update __dict__ and then call vars()d.__dict__['id'] = 100print(vars(d))

Output:

输出:

{'id': 1, 'name': 'Pankaj', 'repr': 'Data[1,Pankaj]'}{'id': 100, 'name': 'Pankaj', 'repr': 'Data[1,Pankaj]'}

Let’s see the vars() function output with the class as an argument.

让我们看看以类作为参数的vars()函数输出。

print(vars(Data))

Output:

输出:

{'__module__': '__main__', 'id': 0, 'name': '', '__init__': 
, '__dict__':
, '__weakref__':
, '__doc__': None}

带有模块的vars() (vars() with module)

import mathprint(vars(math))

Output:

输出:

{'__name__': 'math', '__doc__': 'This module is always available.  It provides access to the\nmathematical functions defined by the C standard.', '__package__': '', '__loader__': <_frozen_importlib_external.ExtensionFileLoader object at 0x1085654a8>, '__spec__': ModuleSpec(name='math', loader=<_frozen_importlib_external.ExtensionFileLoader object at 0x1085654a8>, origin='/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'), 'acos': 
, 'acosh':
, 'asin':
, 'asinh':
, 'atan':
, 'atan2':
, 'atanh':
, 'ceil':
, 'copysign':
, 'cos':
, 'cosh':
, 'degrees':
, 'erf':
, 'erfc':
, 'exp':
, 'expm1':
, 'fabs':
, 'factorial':
, 'floor':
, 'fmod':
, 'frexp':
, 'fsum':
, 'gamma':
, 'gcd':
, 'hypot':
, 'isclose':
, 'isfinite':
, 'isinf':
, 'isnan':
, 'ldexp':
, 'lgamma':
, 'log':
, 'log1p':
, 'log10':
, 'log2':
, 'modf':
, 'pow':
, 'radians':
, 'remainder':
, 'sin':
, 'sinh':
, 'sqrt':
, 'tan':
, 'tanh':
, 'trunc':
, 'pi': 3.141592653589793, 'e': 2.718281828459045, 'tau': 6.283185307179586, 'inf': inf, 'nan': nan, '__file__': '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'}

vars()不带参数 (vars() without argument)

print(vars())

Output:

输出:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x108508390>, '__spec__': None, '__annotations__': {}, '__builtins__': 
, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_vars_function.py', '__cached__': None, 'Data':
, 'd': <__main__.Data object at 0x108565048>, 'math':
}
. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

python vars()

转载地址:http://pxmzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>