网络编程
位置:首页>> 网络编程>> Python编程>> python中自带的三个装饰器的实现

python中自带的三个装饰器的实现

作者:我爱学python  发布时间:2021-10-28 02:31:52 

标签: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)

来源:https://www.jianshu.com/p/c07bee9269cd

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com