装饰器property的简单运用

property函数:在类中使用,将类中的方法伪装成一个属性

使用方法:在函数,方法,类的上面一行直接@装饰器的名字

装饰器的分类:

  装饰器函数

  装饰器方法:property

  装饰类

class Student:
    def __init__(self,name):
        self.__name = name
    @property
    def name(self):
        return self.__name
    @name.setter       #设置,修改(自我理解)
    def name(self,new_name):  
        if type(new_name) is str:  #只有当修改的值为str类型,才能被修改
            self.__name = new_name
a1 = Student("诸葛")
print(a1.name)      #诸葛
a1.name = "睿智"
print(a1.name)      #睿智
a1.name = 123
print(a1.name)      #睿智

setter的用法:

  setter是只有被property方法之后的,又实现了一个同名的方法,且被setter装饰器装饰了

   它的作用是用来保护一个变量,在修改的时候能够添加一些保护条件。

deleter的用法:

  一个方法被伪装成属性后,应该可以执行一个属性的增删改查操作,

  所以deleter就是对应着被deleter装饰的方法,这个方法并不是只为了删除这个属性,而是你在代码中执行什么就有什么效果。

class Goods:
    __discount = 0.8
    def __init__(self,price):
        self.__price = price
    @property
    def price(self):
        return self.__price * self.__discount
    @price.setter
    def price(self,new):
        if type(new) is int:
            self.__price =  new
    @price.deleter
    def price(self):
        del self.__price
apple = Goods(10)
print(apple.price)      #8.0
print(apple.__dict__)   #{'_Goods__price': 10}
apple.price = 20        #将__price的值进行修改
print(apple.price)      #16
print(apple.__dict__)   #{'_Goods__price': 20}
del apple.price         #删除
print(apple.__dict__)    #{}

deleter通常用在

class A:
    def __init__(self):
        self.__f = open('aaa','w')

    @property
    def f(self):
        return self.__f

    @f.deleter
    def f(self):
        self.__f.close()    #先关闭文件
        del self.__f          #删除文件

@classmethod 将类中的方法变成类方法,为了不创建类对象,而直接用类方法修改静态私有属性。用法如下

  只使用类中的资源,且这个资源可以直接用类名引用的使用,那这个方法在方法上面@classmethod将这个方法变成类方法

class Goods:
    __discount = 0.8    #静态私有属性
    def __init__(self,price):
        self.__price = price  #私有对象属性
        self.name = "apple"     #对象属性
    @property
    def price(self):
        return self.__price *Goods.__discount

    @classmethod        #类方法
    def change_disount(cls,new):     #cls 表示Goods这个类
        cls.__discount = new    #对Goods中的静态私有属性进行修改
print(Goods.__dict__)       # '_Goods__discount': 0.8,
Goods.change_disount(0.7)
print(Goods.__dict__)       #'_Goods__discount': 0.7,                     

@staticmethod 静态方法  (如果函数要在类中使用,就用静态方法)

class Student:

    @staticmethod    #在类中创建函数
    def login(usr,pwd):
        print('IN LOGIN',usr,pwd)


Student.login('user','pwd')    

总结

#  类:
    # 静态属性         类  所有的对象都统一拥有的属性
    # 类方法           类  如果这个方法涉及到操作静态属性、类方法、静态方法                            cls 表示类
    # 静态方法         类  普通方法,不使用类中的命名空间也不使用对象的命名空间   : 一个普通的函数    没有默认参数
    # 方法             对象                                                                            self 表示对象
    # property方法     对象                                                                            slef 表示对象

isinstance(a,A)  判断对象与类直接的关系

issubclass(A,B)     判断类与类之间的关系

class A:pass
class B(A):pass
a = A()
b = B()
# print(type(a) is A)     #True
# print(type(b) is B)     #True
# print(type(b) is A)     #False

# print(isinstance(a,A))  #True   #isinstance判断对象a与类A的关系(自我理解)
# print(isinstance(b,A))  #True
# print(isinstance(a,B))  #False
print(issubclass(B,A))  #True
print(issubclass(A,B))  #False  判断类与类之间的关系

  

  

原文地址:https://www.cnblogs.com/yuncong/p/9562784.html