本文共 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() function syntax is:
Python vars()函数语法为:
vars([object])
TypeError
exception is thrown with message TypeError: vars() argument must have __dict__ attribute
. 如果指定的参数不具有__dict__属性,则会引发TypeError
异常并显示消息TypeError: vars() argument must have __dict__ attribute
。 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}
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'}
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': }
Reference:
参考:
翻译自:
python vars()
转载地址:http://pxmzd.baihongyu.com/