python自定制classmethod (类方法)
Posted Mr-谢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python自定制classmethod (类方法)相关的知识,希望对你有一定的参考价值。
利用描述符和类装饰器自定义classmethod方法
class ClassMethod: def __init__(self,func): self.func = func def __get__(self, instance, owner): def deco(*args,**kwargs): self.func(owner,*args) return deco class People: name = ‘qqq‘ @ClassMethod #sayHello = ClassMethod(sayHello) def sayHello(cls,msg): print("你好, %s 帅哥 %s" %(cls.name,msg)) @classmethod def sayHello1(cls,msg): print("你好 %s %s" %(cls.name,msg)) p1 = People() p1.sayHello1(‘sadsad‘) p1.sayHello(‘dsadwqdqw‘) People.sayHello(‘safsaf‘) People.sayHello1(‘sfdsfdsf‘)
以上是关于python自定制classmethod (类方法)的主要内容,如果未能解决你的问题,请参考以下文章
python类方法@classmethod与@staticmethod
python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)多态封装的特性property
Python类总结-ClassMethod, StaticMethod
面试必问python实例方法类方法@classmethod静态方法@staticmethod和属性方法@property区别