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正则匹配抓取豆瓣电影链接和评论代码分享
Dec 27 Python
pygame学习笔记(2):画点的三种方法和动画实例
Apr 15 Python
python分析网页上所有超链接的方法
May 08 Python
python2.7的编码问题与解决方法
Oct 04 Python
python 将数据保存为excel的xls格式(实例讲解)
May 03 Python
python实现归并排序算法
Nov 22 Python
对python的unittest架构公共参数token提取方法详解
Dec 17 Python
Python的bit_length函数来二进制的位数方法
Aug 27 Python
Python Celery多队列配置代码实例
Nov 22 Python
Python pytesseract验证码识别库用法解析
Jun 29 Python
django rest framework使用django-filter用法
Jul 15 Python
基于OpenCV的网络实时视频流传输的实现
Nov 15 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
php microtime获取浮点的时间戳
2010/02/21 PHP
Base64在线编码解码实现代码 演示与下载
2011/01/08 PHP
第4章 数据处理-php字符串的处理-郑阿奇(续)
2011/07/04 PHP
php操作JSON格式数据的实现代码
2011/12/24 PHP
PHP的Yii框架使用中的一些错误解决方法与建议
2015/08/21 PHP
分享50个提高PHP执行效率的技巧
2015/12/26 PHP
PHP进阶学习之依赖注入与Ioc容器详解
2019/06/19 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
2019/10/01 PHP
js类后台管理菜单类-MenuSwitch
2007/09/12 Javascript
jquery插件 cluetip 关键词注释
2010/01/12 Javascript
document.getElementById为空或不是对象的解决方法
2010/01/24 Javascript
JavaScript CSS修改学习第六章 拖拽
2010/02/19 Javascript
下载网站打开页面后间隔多少时间才显示下载链接地址的代码
2010/04/25 Javascript
json的前台操作和后台操作实现代码
2012/01/20 Javascript
jQuery页面加载初始化常用的三种方法
2014/06/04 Javascript
JS在IE下缺少标识符的错误
2014/07/23 Javascript
HTML5+jQuery插件Quicksand实现超酷的星际争霸2兵种分类展示效果(附demo源码下载)
2016/05/25 Javascript
JavaScript中值类型和引用类型的区别
2017/02/23 Javascript
从零学习node.js之详解异步控制工具async(八)
2017/02/27 Javascript
webpack实现一个行内样式px转vw的loader示例
2018/09/13 Javascript
vue实现滑动超出指定距离回顶部功能
2019/07/31 Javascript
jQuery实现本地存储
2020/12/22 jQuery
利用python发送和接收邮件
2016/09/27 Python
CentOS 6.5中安装Python 3.6.2的方法步骤
2017/12/03 Python
mac系统安装Python3初体验
2018/01/02 Python
python通过elixir包操作mysql数据库实例代码
2018/01/31 Python
Django实现跨域的2种方法
2019/07/31 Python
python help函数实例用法
2020/12/06 Python
英国独特的时尚和生活方式品牌:JOY
2018/03/17 全球购物
JavaScript实现页面动态验证码的实现示例
2021/03/23 Javascript
2014年应届大学生自我评价
2014/01/09 职场文书
廉洁自律承诺书2015
2015/01/22 职场文书
2015年办公室人员工作总结
2015/05/15 职场文书
行政处罚告知书
2015/07/01 职场文书
导游词之秦始皇兵马俑博物馆
2019/09/29 职场文书
mysql数据库隔离级别详解
2022/06/16 MySQL