python dir()和vars()的区别
来源:互联网 发布:小额贷款业务软件 编辑:程序博客网 时间:2024/04/30 07:49
dir():默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的属性
['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}
(完)
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__': <attribute '__dict__' of 'C' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None, 'f': 2})>>> C.__dict__dict_proxy({'__dict__': <attribute '__dict__' of 'C' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'C' objects>, '__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__{}>>>
(完)
1 0
- python dir()和vars()的区别
- python dir()和vars()的区别
- dir()和vars()区别
- python dir & vars
- python中的globals()、locals()、dir()、vars()、__dict__
- Python之dir()与__dict__的区别
- Python之dir()与__dict__的区别
- python里使用vars()返回当前局部作用域的所有成员函数和成员变量
- console.dir()和console.log()的区别
- console.dir()和console.log()的区别
- Python dir() 和 __dir__
- Python的dir函数
- Python的dir()函数
- python基础-dir()和__dict__
- python中的dir()和__dict__
- spark.eventLog.dir和spark.history.fs.logDirectory的区别
- Python的type, str, dir和callable函数
- Python中有用的三个工具:dir, type和help
- UVa 100 3n+1问题 The 3n + 1 problem
- Oracle备份恢复-手动备份
- 数位动态规划(acm hdu 3555 ,hdu 2089,hdu 4278)
- STL之set集合容器
- requset.setAttribute(),session.setAttribute(),aplication.setAttribute()区别
- python dir()和vars()的区别
- 数据结构实验之栈三:后缀式求值
- iOS CoreData简单封装
- mybatis-generator学习入门
- Oracle备份恢复-手动完全恢复
- uva131 The Psychic Poker Player
- CSS文本样式[上]
- python笔记之NUMPY中的meshgrid()——生成以某点为中心指定半径内的
- 仿iOS的对话框