class A(object):
_c ="test"
def __init__(self):
print '__init__'
self.x = None
@property
def a(self):
print "using property to access attribute"
if self.x is None:
print "return value"
else:
print "error occured"
raise AttributeError
@a.setter
def a(self,value):
print 'setter',value
self.x = value
def __getattr__(self, name):
print 'using __getattr__ to access attribute',name
return "b"
def __getattribute__(self, name):
print "using __getattribute__ to access attribute",name
return object.__getattribute__(self,name)
a1 = A()
print a1.a
print '-'*10
a1.a =1
print a1.a
print '*'*10
print a1._c
输出:
__init__
using __getattribute__ to access attribute a
using property to access attribute
using __getattribute__ to access attribute x
return value
None
----------
setter 1
using __getattribute__ to access attribute a
using property to access attribute
using __getattribute__ to access attribute x
error occured
using __getattr__ to access attribute a
b
**********
using __getattribute__ to access attribute _c
test