python中常用的装饰器有以下几种
@property
@property是python的一种装饰器,常用于用来修饰方法。
class DataSet(object):
@property
def method_with_property(self):
return 15
def method_without_property(self):
return 15
l = DataSet()
print(l.method_with_property)
print(l.method_without_property())
@abstractmethod
@abstractmethod装饰器是一种抽象方法,表示基类。
from abc import ABC, abstractmethod
class Foo(ABC):
@abstractmethod
def fun(self):
'''please Implemente in subclass'''
class SubFoo(Foo):
def fun(self):
print('fun in SubFoo')
a = SubFoo()
a.fun()
@staticmethoed
@staticmethoed装饰器不需要表示自身对象的self和自身类的cls参数。
class A(object):
bar = 1
def foo(self):
print 'foo'
@staticmethod
def static_foo():
print 'static_foo'
print A.bar
@classmethod
def class_foo(cls):
print 'class_foo'
print cls.bar
cls().foo()
A.static_foo()
A.class_foo()