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

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

dir():默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的属性

vars():默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的属性

vars():函数以字典形式返回参数中每个成员的当前值,如果vars函数没有带参数,那么它会返回包含当前局部命名空间中所有成员的当前值的一个字典。

>>> help(vars)

Help on built-in function vars in module __builtin__:
vars(...)
    vars([object]) -> dictionary 
    Without arguments, equivalent to locals().
    With an argument, equivalent to object.__dict__.

dir()和vars()的区别就是:dir()只打印属性,vars()则打印属性与属性的值。

a='abcdefg'class B():    c='djfj'print dir()print vars()print dir(B)print vars(B)
结果:
['B', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a']
{'a': 'abcdefg', 'B': <class __main__.B at 0x02A2DD88>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'E:\\workspace\\python day03\\main\\test.py', '__package__': None, '__name__': '__main__', '__doc__': None}
['__doc__', '__module__', 'c']
{'__module__': '__main__', 'c': 'djfj', '__doc__': None}

>>> class C(object):	    f=2	>>> dir(C)['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'f']>>> vars(C)dict_proxy({'__dict__': 
, '__module__': '__main__', '__weakref__':
, '__doc__': None, 'f': 2})>>> C.__dict__dict_proxy({'__dict__':
, '__module__': '__main__', '__weakref__':
, '__doc__': None, 'f': 2})>>> c=C()>>> dir(c)['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'f']>>> vars(c){}>>> c.__dict__{}>>>
(完)

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

你可能感兴趣的文章
Debian 7.1 x64 - 使用Oracle SQL Developer 3.2.2 (3.2.20.09.87)
查看>>
Oracle EBS R12 - ad patch/non-ad patch/admrgpch
查看>>
Oracle EBS R12 - Application patch可不可以reapply
查看>>
rh423 - redhat-ds如何移除已有ds并重新部署新的ds
查看>>
rh423 - 主辅同步报错:there is no replicated area "dc=server119,dc=example,dc=com" on the consumer server
查看>>
rh423 - Linux客户端利用winbind加入Windows 2008 Enterprise R1/SP1的AD
查看>>
2021-04-01
查看>>
2021-04-01
查看>>
matlab中调用函数
查看>>
leetcode - 只出现一次的数字
查看>>
leetcode - 旋转数组
查看>>
ubuntu忘记密码?怎么办
查看>>
ubuntu系统解决按上下健等出现字母问题
查看>>
ubuntu镜像下载地址
查看>>
ubuntu下安装gcc
查看>>
运行 apt-get 更新时 lgn、Hit、Get 的意思
查看>>
socket 编程时 vs2017 对 inet_addr 和 inet_ntoa 的报错
查看>>
什么是套接口?
查看>>
Ubuntu18.04安装中文输入法
查看>>
解决ubuntu18.04系统桌面很卡的问题
查看>>