1.属性访问的例子:
class Person:
def init(self,name):
self._name=name
def getattr(self,attr):
print('get:'+attr)
if attr=='name':
return self._name
else:
raise AttributeError(attr)
def setattr(self,attr,value):
print('set:'+attr +' '+value)
if attr=='name':
attr='_name'
self.dict[attr]=value
bob=Person('Bob')
print(bob.name)
bob.name='2222'
print(bob.name)


网友评论