魔术方法之描述符

#
# @File  :
# @Author: dianxiaoer
# @Date  : 2019/11/5
# @Desc  :

# __getattr__() 定义当用户试图访问一个不存在的属性时的行为
# __getattribute__() 定义当该类的属性被访问时的行为

# class C:
#     def __getattribute__(self, name):
#         print("getattribute")
#         return super().__getattribute__(name)
#     def __getattr__(self, name):
#         print("getattr")
#     def __setattr__(self, name, value):
#         print("setattr")
#         super().__setattr__(name,value)
#     def __delattr__(self, name):
#         print("delattr")
#         super().__delattr__(name)
#
# c = C()
# print(c.x)
# print("*"*10)
# c.x = 1
# print("*"*10)
# del c.x
# print(c.x)

# 死循环陷阱
# 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:
#             self.name = value
#
#     def getArea(self):
#         return self.width*self.height
#
# r1 = Rectangle(4,5)

# 死循环陷阱改进
# 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) 推荐方式
#             self.__dict__[name] = value
#
#     def getArea(self):
#         return self.width * self.height
#
#
# r1 = Rectangle(4, 5)
# print(r1.getArea())
# print("*" * 10)
# r1.square = 10
# print(r1.height)
# print(r1.getArea())
# print(r1.__dict__)

# 描述符:将某种特殊类型的类的实例指派给另一个类的属性
# __get__() # 用于访问属性,它返回属性的值
# __set__() # 将属性在分配操作中使用,不返回任何的值
# __delete__() # 控制删除操作,不返回任何内容

# class MyDecriptor:
#     def __get__(self, instance, owner):
#         print("getting……",self,instance,owner)
#
#     def __set__(self, instance, value):
#         print("setting……",self,instance,value)
#
#     def __delete__(self, instance):
#         print("delete……",self,instance)
#
# class Test:
#     x = MyDecriptor()
#
# test = Test()
# print(test.x)
# print(test)
# print("*"*10)
# test.x = "diaoxiaoer"
# print("*"*10)
# del test.x

# class MyProperty:
#     def __init__(self,fget=None,fset=None,fdel=None):
#         self.fget = fget
#         self.fset = fset
#         self.fdel = fdel
#
#     def __get__(self, instance, owner):
#         return self.fget(instance)
#
#     def __set__(self, instance, value):
#         self.fset(instance,value)
#
#     def __delete__(self, instance):
#         self.fdel(instance)
#
# class C:
#     def __init__(self):
#         self.x = None
#
#     def getX(self):
#         return self._x
#
#     def setX(self,value):
#         self._x = value
#
#     def delX(self,instance):
#         del self._x
#
#     x = MyProperty(getX,setX,delX)
#
# c = C()
# c.x = "dianxiaoer"
# print(c.x)
############################################################
# class Celsius:
#     def __init__(self,value = 26.0):
#         self.value = value
# 
#     def __get__(self, instance, owner):
#         return self.value
#
#     def __set__(self, instance, value):
#         self.value = float(value)
#
# class Fahrenheit:
#     def __get__(self, instance, owner):
#         return instance.cel*1.8 + 32
#
#     def __set__(self, instance, value):
#         instance.cel = float(value-32)/1.8
#
# class Temperature:
#     cel = Celsius()
#     fah = Fahrenheit()
#
# temp = Temperature()
# print(temp.cel)
# print("**********************************")
# temp.cel = 30
# print(temp.fah)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值