码迷,mamicode.com
首页 > 编程语言 > 详细

python属性访问

时间:2017-08-17 00:42:45      阅读:118      评论:0      收藏:0      [点我收藏+]

标签: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=1

class 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.height

r1=rectangle(4,5)#实例化类
print(r1.getArea())
r1.square=10#说明赋予了一个square属性,则直接把square赋值给value,执行操作,就是正方形
print(r1.getArea())
print(r1.__dict__)#直接打印出了字典属性

技术分享

 

python属性访问

标签:utf-8   ict   turn   http   实例   font   ini   images   bin   

原文地址:http://www.cnblogs.com/aqiuarcadia/p/7376516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!