__getattr__和__setattt__使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了__getattr__和__setattt__使用相关的知识,希望对你有一定的参考价值。

# coding:utf-8
"""
__setattr__(self, name, value),如果要给name赋值,调用此方法
__getattr__(self, name) 如果name被访问,同时它不存在的时候,此方法被调用
__getattribute__(self, name)  当name被访问时自动被调用(这个进用于新类式),无论name是否存在,都要被调用
__delattr__(self, name) 如果要删除name,这个方法就被调用

"""

class A(object):
    def __getattr__(self, name):    # 读属性,属性不存在时,调用此方法,即所谓的“拦截成员”
        print "You use __getattr__"

    def __setattr__(self, name, value): #给对象赋值时,调用此方法
        print "You use __setattr__"
        self.__dict__[name] = value

class B(object):
    s = adg
    def __getattribute__(self, name):# 新式类,访问属性就掉哦有那个它
        print "You are using __getattribute__"
        return object.__getattribute__(self, name)
        # 返回值没有调用return self.__dict__[name].因为如果调用这样的凡事就会访问self.__dict__,
        # 只要访问这个属性,就要调用__getattribute__,这样就导致了无限递归下去成死循环

#例子
class Rectangle(object):
    """the width and length of Rectangle"""
    def __init__(self):
        self.width = 0
        self.length = 0
    def setSize(self, size):
        self.width,self.length = size
    def getSize(self):
        return self.width,self.length
r = Rectangle()
r.width = 3
r.length = 4
print r.getSize()
r.setSize( (30, 40) )
print r.width
print r.length

"""改进,使用property函数"""
class Rectangle2(object):
    """the width and length of Rectangle"""
    def __init__(self):
        self.width = 0
        self.length = 0
    def setSize(self, size):
        self.width,self.length = size
    def getSize(self):
        return self.width,self.length
    size = property(getSize, setSize)
print "-------property()-------------"
s = Rectangle2()
s.width = 3
s.length = 4
print s.size
r.size = 30,40
print s.width
print s.length

"""@property,方法变成属性"""
class Rectangle3(object):
    def __init__(self):
        self.width = 0
        self.length = 0
    @property
    def size(self):
        return self.width, self.length
    @size.setter
    def size(self,size):
        self.width,self.length = size

print "[email protected]"
s = Rectangle3()
s.width = 3
s.length = 4
print s.size
s.size = 30,40
print s.width
print s.width

"""使用setattr和getattr"""
class Rectangle4(object):
    def __init__(self):
        self.width = 0
        self.length = 0

    def __setattr__(self, key, value):
        if key  == "size":
            self.width, self.length = value
        else:
            self.__dict__[key] = value
    def __getattr__(self, item):
        if item == "size":
            return self.width, self.length
        else:
            raise AttributeError
print "------------setattr,getattr---------"
s = Rectangle4()
s.width = 3
s.length = 4
print s.size
s.size = 30,40
print r.width
print r.length

结果:

(3, 4)
30
40
-------property()-------------
(3, 4)
3
4
[email protected]
(3, 4)
30
30
------------setattr,getattr---------
(3, 4)
30
40

 

以上是关于__getattr__和__setattt__使用的主要内容,如果未能解决你的问题,请参考以下文章

访问全局变量时模块的 __setattr__ 和 __getattr__

Python的__getattr__和__getattribute__

python中__getattr__和__getattribute__区别

__setattr__和__delattr__和__getattr__

Django HStore:如何覆盖键值模型字段的 __getattr__ 和 __setattr__

python 类中__getattr__的使用