python vars()_Python vars()
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 dictionary.
Python vars()函数为模块,类,实例或具有__dict__属性的任何其他对象返回__dict__属性。 因此,vars()函数的输出是一个dictionary 。
Python vars() (Python vars())
Python vars() function syntax is:
Python vars()函数语法为:
vars([object])
- If no argument is provided, vars() act like locals() function.如果未提供任何参数,则vars()的作用类似于locals()函数。
- 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 messageTypeError: 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 variablesid = 0name = ''def __init__(self, i, n):self.id = iself.name = n# instance variableself.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 object
print(vars(d))# update __dict__ and then call vars()
d.__dict__['id'] = 100
print(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__': <function Data.__init__ at 0x108566158>, '__dict__': <attribute '__dict__' of 'Data' objects>, '__weakref__': <attribute '__weakref__' of 'Data' objects>, '__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': <built-in function acos>, 'acosh': <built-in function acosh>, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan': <built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in function copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>, 'degrees': <built-in function degrees>, 'erf': <built-in function erf>, 'erfc': <built-in function erfc>, 'exp': <built-in function exp>, 'expm1': <built-in function expm1>, 'fabs': <built-in function fabs>, 'factorial': <built-in function factorial>, 'floor': <built-in function floor>, 'fmod': <built-in function fmod>, 'frexp': <built-in function frexp>, 'fsum': <built-in function fsum>, 'gamma': <built-in function gamma>, 'gcd': <built-in function gcd>, 'hypot': <built-in function hypot>, 'isclose': <built-in function isclose>, 'isfinite': <built-in function isfinite>, 'isinf': <built-in function isinf>, 'isnan': <built-in function isnan>, 'ldexp': <built-in function ldexp>, 'lgamma': <built-in function lgamma>, 'log': <built-in function log>, 'log1p': <built-in function log1p>, 'log10': <built-in function log10>, 'log2': <built-in function log2>, 'modf': <built-in function modf>, 'pow': <built-in function pow>, 'radians': <built-in function radians>, 'remainder': <built-in function remainder>, 'sin': <built-in function sin>, 'sinh': <built-in function sinh>, 'sqrt': <built-in function sqrt>, 'tan': <built-in function tan>, 'tanh': <built-in function tanh>, 'trunc': <built-in function 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__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_vars_function.py', '__cached__': None, 'Data': <class '__main__.Data'>, 'd': <__main__.Data object at 0x108565048>, 'math': <module 'math' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'>}
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/23151/python-vars
python vars()
python vars()_Python vars()相关推荐
- python对象_Python对象()
python对象 Python object() function returns a new featureless object. Python is an object-oriented pro ...
- python 并发_Python bin()
python 并发 Python bin() function is used to convert an integer into the binary format string. The for ...
- python断点_Python断点()
python断点 Python breakpoint() is a new built-in function introduced in Python 3.7. Python code debugg ...
- python属性_Python属性()
python属性 Python property() function returns a property attribute. It's mostly used to create a manag ...
- python type函数_Python type()函数
python type函数 Python type()函数 (Python type() Function) Python has a lot of buit-in function. The typ ...
- Python中的super()函数
多路继承的问题 描述: 解决这样的问题Python中可以使用super() super()函数有点: (1)在父类中可以直接的调用未绑定的方法 (2)在确保所有的父类的构造方法都使用了super()函 ...
- python如何创建一个列表_使用python中的format()创建一个列表(make a list using format() in python)...
使用python中的format()创建一个列表(make a list using format() in python) 我是python和编码的新手. 因此,如果已经讨论过这件事,我很抱歉,我无 ...
- 使用Python调用opencv学习(-)打开图片,显示图片
使用Python调用opencv学习(-)打开图片,显示图片 第一步是读取图片是用cv2.imread 第一个参数是图片的路径可以是绝对路径也可以是相对路径.第二个参数代表读取图片的格式 cv2.IM ...
- python pop_Python清单pop()方法
python pop 介绍 (Introduction) Today we'll be going the Python list pop() method. We generally have va ...
- python中的eval()方法
在python中,eval()方法是一个经常用到的函数,我们在编写输入函数的时候,需要把input()函数写进eval()方法中,这样得到的输入结果就不会是字符串类型的了. 例如: a=input(' ...
最新文章
- mysql数据库实现主从复制
- 如何通过Windows Server 2008 R2建立NFS存储
- Struts2源码阅读(三)_DispatcherConfigurationProvider
- linux c语言编写聊天室mysql_Linux平台上用C语言实现与MySQL数据库的连接
- Java Web-面试题
- android动画的实现过程
- 「开源资讯」浏览器中可以深度学习的框架Paddle.js 1.0 发布
- centos下mysql备份数据库命令_[CentOS]下mysql数据库常用命令总结
- 纷杂的Spring-boot-starter: 3 数据访问与spring-boot-starter-jdbc
- java服装销售系统课程设计_毕业论文(设计)基于javaweb的服装销售管理系统的设计与实现.doc...
- MATLAB数字信号处理系统GUI实现
- oracle11g卸载出错 无法删除文件,文件正在使用中
- chat--hxxdfd
- hyperLynx VX2.5 PCB仿真
- 【DB笔试面试758】在Oracle的DG中,Switchover和Failover的区别有哪些?
- 微信公众号推送课表及天气(事无巨细+JavaScript版+python版)
- BIOS Setup设置方法
- Oracle查询某个日期的周一到周日SQL语句
- 阿里巴巴的机器视觉有多强!ET城市大脑发布四大AI视觉产品
- 彻底放弃了一直钟爱的紫光输入法
热门文章
- 数据结构-1-顺序表的实现
- [转载] Python 列表表达式
- [转载] [硕.Love Python] QuickSort(快速排序)
- [转载] python面面观单元测试_python 使用unittest进行单元测试
- [转载] JAVA中分为基本数据类型及引用数据类型
- [置顶] Responder一点也不神秘————iOS用户响应者链完全剖析
- Springboot 使用wangEditor3.0上传图片
- EntityFramework6.X 之 Operation
- SpringData环境搭建代码编写
- VMWare关闭beep声