圆月山庄资源网 Design By www.vgjia.com
比如下面的例子: class Book(object):
    def __setattr__(self, name, value):
        if name == 'value':
            object.__setattr__(self, name, value - 100)
        else:
            object.__setattr__(self, name, value)
    def __getattr__(self, name):
        try:
            return object.__getattribute__(name)
        except:
            return name + ' is not found!'
    def __str__(self):
        return self.name + ' cost : ' + str(self.value)

c = Book()
c.name = 'Python'
c.value = 100
print c.name
print c.value
print c
print c.Type


上面的例子中,在赋值书的value属性时,偷偷的将value减去了100,呵。输出结果:

Python
0
Python cost : 0
Type is not found!
标签:
Python,__getattr__,__setattr__

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com