classmethod 和 staticmethod 工作原理

classmethod 

class Classmethod:
    def __init__(self,func):
        self.func=func
    def __get__(self, instance, owner):
        def test(*args,**kwargs):
            return self.func(owner,*args,**kwargs)
        return test

class Cq:
    red=1
    @Classmethod  #test=Classmethod(test)
    def test(cls):
        return cls.red
print(Cq.test())

staticmethod

class Staticmethod:
    def __init__(self,func):
        self.func=func
    def __get__(self, instance, owner):
        def nat(*args,**kwargs):
            return self.func(*args,**kwargs)
        return nat

class Coo:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    @Staticmethod #test=Staticmethod(test)
    def test(name2,age1):
        s=2
        return s

c=Coo("ljh",18)
print(c.test("ljh",19))

 

posted @ 2020-07-11 18:22  彡心如止水彡  阅读(164)  评论(0)    收藏  举报