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

Posted xieys-1993

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过描述符和类装饰器,自定义staicmethod(静态方法)相关的知识,希望对你有一定的参考价值。

通过描述符和类装饰器,自定义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))

 

以上是关于通过描述符和类装饰器,自定义staicmethod(静态方法)的主要内容,如果未能解决你的问题,请参考以下文章