python屬性訪問

來源:互聯網
上載者:User

標籤:utf-8   ict   turn   http   執行個體   font   ini   images   bin   

#! /usr/bin/env python#! -*- cording:utf-8 -*-## class C:#     def __getattribute__(self, name):#         print("getattribute")#         return super().__getattribute__(name)#super預設找父類#     def __getattr__(self, name):#         print("getattr")#     def __setattr__(self, name, value):#         print("setatter")#         super().__setattr__(name,value)#     def __delattr__(self, name):#         print("delattr")#         super().__delattr__(name)# c=C()# c.x## c.x=1class rectangle:    def __init__(self,width=0,height=0):        self.width=width        self.height=height    def __setattr__(self, name, value):        if name=="square":            self.width=value            self.height=value        else:            super().__setattr__(name,value)    def getArea(self):        return  self.width*self.heightr1=rectangle(4,5)#執行個體化類print(r1.getArea())r1.square=10#說明賦予了一個square屬性,則直接把square賦值給value,執行操作,就是正方形print(r1.getArea())print(r1.__dict__)#直接列印出了字典屬性

 

python屬性訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.