经典类属性和新式类属性

Posted Howhy Blogs

tags:

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

经典类只@property 没有setter 和deleter

 

class A(object):
    def __init__(self,price):
        self.__price=price
    @property    
    def price(self):
        return self.__price
    @price.setter
    def price(self,value):
        self.__price=value
    @price.deleter
    def price(self):
        print ‘@price.deleter‘
a=A(1.23)
print(a.price)
a.price
=23.4
print(a.price)
del a.price

结果:
1.23
23.4
del attr
 

 

以上是关于经典类属性和新式类属性的主要内容,如果未能解决你的问题,请参考以下文章

继承派生新式类与经典类

python中的__new__与__init__,新式类和经典类(2.x)

新式类 VS 经典类

属性的两种定义方式

继承1.继承,2.属性查找顺序3.子类访问父类内容4.补充,经典类与新式类 --------- # 21

python-- 类的继承(新式类/经典类)多态