Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。
__dict__与dir()
的区别:
__dict__是一个字典,键为属性名,值为属性值;
dir()是一个函数,返回的是列表 list,dir()用来寻找一个对象的所有属性,包括__dict__中的属性,__dict__是dir()的子集;
并不是所有对象都拥有__dict__属性。许多内建类型就没有__dict__属性,如 lis t,此时就需要用dir()来列出对象的所有属性。
__dict__
属性 __dict__
是用来存储对象属性的一个字典,其键为属性名,值为属性的值。
#!/usr/bin/python
# -*- coding: utf-8 -*-
class A(object):
class_var = 1
def __init__(self):
self.name = 'xy'
self.age = 2
@property
def num(self):
return self.age + 10
def fun(self):pass
def static_f():pass
def class_f(cls):pass
if __name__ == '__main__':#主程序
a = A()
print a.__dict__ # {'age': 2, 'name': 'xy'} 实例中的__dict__属性
print A.__dict__
'''
类A的__dict__属性
{
'__dict__': , #这里如果想深究的话查看参考链接5
'__module__': '__main__', #所处模块
'num': , #特性对象
'class_f': , #类方法
'static_f': , #静态方法
'class_var': 1,
'fun': , #类变量
'__weakref__': ,
'__doc__': None, #class说明字符串
'__init__':
}
'''
a.level1 = 3
a.fun = lambda :x
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy','fun': at 0x> }
print A.__dict__ #与上述结果相同
A.level2 = 4
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy'}
print A.__dict__ #增加了level2属性
print object.__dict__
'''
{'__setattr__': ,
'__reduce_ex__': ,
'__new__': ,
.....
'''
从上述代码可知,
实例的__dict__
仅存储与该实例相关的实例属性,
正是因为实例的__dict__
属性,每个实例的实例属性才会互不影响。
类的__dict__
存储所有实例共享的变量和函数(类属性,方法等),类的__dict__
并不包含其父类的属性。
dir()是Python提供的一个API函数,dir()函数会自动寻找一个对象的所有属性(包括从父类中继承的属性)。
一个实例的__dict__
属性仅仅是那个实例的实例属性的集合,并不包含该实例的所有有效属性。所以如果想获取一个对象所有有效属性,应使用dir()。
print dir()
print "--------------"
print dir(A)
print "--------------"
print dir(a)
print "--------------"
print dir(object)
"""
['A', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a']
--------------
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'class_f', 'class_var', 'fun', 'num', 'static_f']
--------------
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'age', 'class_f', 'class_var', 'fun', 'name', 'num', 'static_f']
--------------
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
"""
#因为每个类都有一个__doc__属性,所以需要去重,去重后然后比较
print set(dir(a)) == set(a_dict + A_dict + object_dict) #True
dir()函数会自动寻找一个对象的所有属性,包括__dict__
中的属性。
__dict__
是dir()的子集,dir()包含__dict__
中的属性。
补充:(类中私有属性的访问)
# -*- coding: utf-8 -*-
class A(object):
class_var = 1
def __init__(self):
self.name = 'xy'
self.age = 2
self.__big=33
if __name__ == '__main__':#主程序
a = A()
print a.__dict__
print dir(a)
print a._A__big
['_A__big', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'class_f', 'class_var', 'fun', 'name', 'num', 'static_f']
33
结论:
类中的私有属性也是可以访问的。