zoukankan      html  css  js  c++  java
  • python中自带的三个装饰器

    说到装饰器,就不得不说python自带的三个装饰器:

    1、@property 将某函数,做为属性使用

    @property 修饰,就是将方法,变成一个属性来使用。

    class A():
    
    
        @property
        def pfunc(self):
            return self.value
    
        @pfunc.setter
        def pfunc(self,value):
            self.value = value
    
        @property
        def pfunc1(self):
            print('this is property')
    
    if __name__=="__main__":
    
        A.pfunc = 9
        print A.pfunc
        A.pfunc1
    

    2、@classmethod 修饰类的方式

    带修饰类方法:cls做为方法的第一个参数,隐式的将类做为对象,传递给方法,调用时无须实例化。

    普通函数方法:self做为第一个参数,隐式的将类实例传递给方法,调用方法时,类必须实例化。

    class A():
        def func(self,x,y):
            return x * y
    
        @classmethod
        def cfunc(cls,x,y):
            return x * y
    
    if __name__=="__main__":
        print A().func(5,5)
        print A.cfunc(4,5)
     
    

    3、@staticmethod 修饰类的方式

    1)是把函数嵌入到类中的一种方式,函数就属于类,同时表明函数不需要访问这个类

    2)使用修饰服,修饰方法,不需要实例化

    class A():
        def func(self,x,y):
            return x * y
    
    
        @staticmethod
        def sfunc(x,y):
            return x * y
    
    
    if __name__=="__main__":
    
        print A.sfunc(6,5)
    
  • 相关阅读:
    datatime模块
    快速幂
    | 与|| ,& 与&&
    sql----order by
    pandas iterrows()
    黄包车比赛 python学习
    右键git-bash不能使用
    17flask分页
    16flask错误处理
    15跨站请求伪造
  • 原文地址:https://www.cnblogs.com/djdjdj123/p/11807427.html
Copyright © 2011-2022 走看看