首页 > 技术文章 > 通过描述符和类装饰器,自定义staicmethod(静态方法)

xieys-1993 2019-09-27 10:13 原文

通过描述符和类装饰器,自定义staicmethod(静态方法)

class StaticMethod:
    def __init__(self,func):
        self.func = func

    def __get__(self, instance, owner):
        def deco(*args,**kwargs):
            return self.func(*args,**kwargs)
        return deco

class Room:
    @staticmethod
    def area(width,length,higt):
        return width * length * higt

    @StaticMethod
    def area1(width,length,higt):
        return width * length * higt

print(Room.area(10,20,1))
print(Room.area1(10,20,1))
r = Room()
print(r.area(10,20,2))
print(r.area1(10,20,2))

 

推荐阅读