from functools import partial
# 类静态方法装饰器
class StaticMethod:
def __init__(self, fn):
self._fn = fn
def __get__(self, instance, owner):
return self._fn
# 类方法装饰器
class ClassMethod:
def __init__(self, fn):
self._fn = fn
def __get__(self, instance, owner):
return partial(self._fn, owner)
# 测试类
class Test:
def __init__(self, age=18):
self.__age = age
self._city = 'beijing'
@StaticMethod #静态描述器 是 非数据描述器
def stfun():
print("StaticMethod Test.stfun")
@ClassMethod
def clsfun(cls):
# print("ClassMethod", cls.__name__)
print("ClassMethod Test.clsfun")
# 静态方法
Test.stfun()
# 类方法
Test.clsfun()
【执行结果】
StaticMethod Test.stfun
ClassMethod Test.clsfun
【知识点】
非数据描述器
偏函数