Python classmethod装饰器原理及用法解析


Posted in Python onOctober 17, 2020

英文文档:

classmethod(function)

Return a class method for function.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
The @classmethod form is a function decorator ? see the description of function definitions in Function definitions for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.

  标记方法为类方法的装饰器

说明:

1. classmethod 是一个装饰器函数,用来标示一个方法为类方法

2. 类方法的第一个参数是类对象参数,在方法被调用的时候自动将类对象传入,参数名称约定为cls

3. 如果一个方法被标示为类方法,则该方法可被类对象调用(如 C.f()),也可以被类的实例对象调用(如 C().f())

>>> class C:
  @classmethod
  def f(cls,arg1):
    print(cls)
    print(arg1)
    
>>> C.f('类对象调用类方法')
<class '__main__.C'>
类对象调用类方法

>>> c = C()
>>> c.f('类实例对象调用类方法')
<class '__main__.C'>
类实例对象调用类方法

4. 类被继承后,子类也可以调用父类的类方法,但是第一个参数传入的是子类的类对象

>>> class D(C):
  pass

>>> D.f("子类的类对象调用父类的类方法")
<class '__main__.D'>
子类的类对象调用父类的类方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python通过exifread模块获得图片exif信息的方法
Mar 16 Python
在Python的Django框架中实现Hacker News的一些功能
Apr 17 Python
Using Django with GAE Python 后台抓取多个网站的页面全文
Feb 17 Python
整理Python 常用string函数(收藏)
May 30 Python
Python编程图形库之Pillow使用方法讲解
Dec 28 Python
计算机二级python学习教程(3) python语言基本数据类型
May 16 Python
python threading和multiprocessing模块基本用法实例分析
Jul 25 Python
Python调用钉钉自定义机器人的实现
Jan 03 Python
Django+Celery实现动态配置定时任务的方法示例
May 26 Python
keras实现VGG16方式(预测一张图片)
Jul 07 Python
深入浅析pycharm中 Make available to all projects的含义
Sep 15 Python
PyQt5 QThread倒计时功能的实现代码
Apr 02 Python
Python基于staticmethod装饰器标示静态方法
Oct 17 #Python
详解python算法常用技巧与内置库
Oct 17 #Python
Python 操作SQLite数据库的示例
Oct 16 #Python
python Selenium 库的使用技巧
Oct 16 #Python
用Python进行websocket接口测试
Oct 16 #Python
python如何控制进程或者线程的个数
Oct 16 #Python
python利用 keyboard 库记录键盘事件
Oct 16 #Python
You might like
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
PHP将字符分解为多个字符串的方法
2014/11/22 PHP
CodeIgniter配置之SESSION用法实例分析
2016/01/19 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
2017/11/12 PHP
PHP常见加密函数用法示例【crypt与md5】
2019/01/27 PHP
javascript 关于# 和 void的区别分析
2009/10/26 Javascript
十分钟打造AutoComplete自动完成效果代码
2009/12/26 Javascript
js中匿名函数的N种写法
2010/09/08 Javascript
JQUERY的属性选择符和自定义选择符使用方法(二)
2011/04/07 Javascript
在JavaScript中监听IME键盘输入事件
2011/05/29 Javascript
js中字符替换函数String.replace()使用技巧
2011/08/14 Javascript
jquery数组封装使用方法分享(jquery数组遍历)
2014/03/25 Javascript
浅谈Javascript中深复制
2014/12/01 Javascript
JS打字效果的动态菜单代码分享
2015/08/21 Javascript
jquery使用on绑定a标签无效 只能用live解决
2016/06/02 Javascript
JointJS流程图的绘制方法
2018/12/03 Javascript
vue使用高德地图根据坐标定位点的实现代码
2019/08/22 Javascript
jQuery 隐藏/显示效果函数用法实例分析
2020/05/20 jQuery
vue组件讲解(is属性的用法)模板标签替换操作
2020/09/04 Javascript
Django框架中方法的访问和查找
2015/07/15 Python
python解析html提取数据,并生成word文档实例解析
2018/01/22 Python
深入浅析Python 中 is 语法带来的误解
2019/05/07 Python
pycharm双击无响应(打不开问题解决办法)
2020/01/10 Python
keras多显卡训练方式
2020/06/10 Python
15个应该掌握的Jupyter Notebook使用技巧(小结)
2020/09/23 Python
opencv python 对指针仪表读数识别的两种方式
2021/01/14 Python
简单总结CSS3中视窗单位Viewport的常见用法
2016/02/04 HTML / CSS
HTML5探秘:用requestAnimationFrame优化Web动画
2018/06/03 HTML / CSS
Java程序员常见面试题
2015/07/16 面试题
教师实习自我鉴定
2013/12/13 职场文书
小学二年级评语
2014/04/21 职场文书
知识改变命运演讲稿
2014/05/21 职场文书
2014年感恩母亲演讲稿
2014/05/27 职场文书
机械专业技术员求职信
2014/06/14 职场文书
国家税务局领导班子对照检查材料思想汇报
2014/10/04 职场文书
python计算列表元素与乘积详情
2022/08/05 Python