python 舊類中使用property特性的方法

來源:互聯網
上載者:User

標籤:訪問   def   舉例   color   att   set   對象   特殊   object   

在python中,我們可以攔截對象的所有特性訪問。通過這種攔截的思路,我們可以在舊式類中實現property方法。

__getattribute__(self, name) #當特性name被訪問時自動調用(只能在新式類中使用)__getattr__(self, name) #當特性name被訪問且對象沒有相應的特性時被自動調用__setattr__(self, name, value) #當試圖給特性name賦值時會被自動調用__delattr__(self, name) #當試圖刪除特性name時被自動調用#*相比於使用property有點複雜,但是特殊方法用途很廣

下面是舉例:

class Demo5(object):    def __init__(self):        print("這是建構函式")        self._value1 = None        self._value2 = None    def __setattr__(self, key, value):        print("try to set the value of the  %s property" % (key,))        if key == ‘newValue‘:            self._value1, self._value2 = value        else:            print("the property key is not newValue, now create or set it through __dict__")            self.__dict__[key] = value    def __getattr__(self, item):        print("try to get the value of %s " % (item,))        if item == ‘newValue‘:            return self._value1, self._value2        else:            print("the %s item is not exist" % (item,))            #raise AttributeError    def __delattr__(self, item):        if item == ‘newValue‘:            print("delete the value of newValue")            del self._value1            del self._value2        else:            print("delete other values ,the value name is %s" % item)if __name__ == "__main__":    testDemo = Demo5()    print(testDemo.newValue)    testDemo.newValue = "name","helloworld"    print("print the value of property:", testDemo.newValue)    print(testDemo.notExist)    del testDemo.newValue

下面是結果:

這是建構函式try to set the value of the  _value1 propertythe property key is not newValue, now create or set it through __dict__try to set the value of the  _value2 propertythe property key is not newValue, now create or set it through __dict__try to get the value of newValue (None, None)try to set the value of the  newValue propertytry to set the value of the  _value1 propertythe property key is not newValue, now create or set it through __dict__try to set the value of the  _value2 propertythe property key is not newValue, now create or set it through __dict__try to get the value of newValue (‘print the value of property:‘, (‘name‘, ‘helloworld‘))try to get the value of notExist the notExist item is not existNonedelete the value of newValuedelete other values ,the value name is _value1delete other values ,the value name is _value2

**其實,我們可以發現在使用__setatter__ , __getatter__,__delatter__這些介面時會對其他的值造成影響,因為這個是通用的必須調用的介面。

python 舊類中使用property特性的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.