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 06 Python
Python使用百度API上传文件到百度网盘代码分享
Nov 08 Python
Python生成器(Generator)详解
Apr 13 Python
Python3字符串学习教程
Aug 20 Python
Python 登录网站详解及实例
Apr 11 Python
Python的SimpleHTTPServer模块用处及使用方法简介
Jan 22 Python
解决python3爬虫无法显示中文的问题
Apr 12 Python
python字符串string的内置方法实例详解
May 14 Python
如何不用安装python就能在.NET里调用Python库
Jul 12 Python
详解PyTorch手写数字识别(MNIST数据集)
Aug 16 Python
Python三元运算与lambda表达式实例解析
Nov 30 Python
仅用几行Python代码就能复制她的U盘文件?
Jun 26 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.MVC的模板标签系统(二)
2006/09/05 PHP
如何使用动态共享对象的模式来安装PHP
2006/10/09 PHP
php 分库分表hash算法
2009/11/12 PHP
php 编写安全的代码时容易犯的错误小结
2010/05/20 PHP
php中计算未知长度的字符串哪个字符出现的次数最多的代码
2012/08/14 PHP
在yii中新增一个用户验证的方法详解
2013/06/20 PHP
PHP获取文件扩展名的常用方法小结【五种方式】
2018/04/27 PHP
PHP设计模式之外观模式(Facade)入门与应用详解
2019/12/13 PHP
javascript css styleFloat和cssFloat
2010/03/15 Javascript
Moment.js 不容错过的超棒Javascript日期处理类库
2012/04/15 Javascript
Jquery 数组操作大全个人总结
2013/11/13 Javascript
在js文件中写el表达式取不到值的原因及解决方法
2013/12/23 Javascript
使用AngularJS实现表单向导的方法
2015/06/19 Javascript
jQuery插件boxScroll实现图片轮播特效
2015/07/14 Javascript
Bootstrap3制作搜索框样式的方法
2016/07/11 Javascript
jQuery实现导航滚动到指定内容效果完整实例【附demo源码下载】
2016/09/20 Javascript
javascript设计模式之单体模式学习笔记
2017/02/15 Javascript
vue项目常用组件和框架结构介绍
2017/12/24 Javascript
基于openlayers4实现点的扩散效果
2020/08/17 Javascript
vue elementUI table表格数据 滚动懒加载的实现方法
2019/04/04 Javascript
Openlayers实现扩散的动态点(水纹效果)
2020/08/17 Javascript
使用Python进行二进制文件读写的简单方法(推荐)
2016/09/12 Python
python实现傅里叶级数展开的实现
2018/07/21 Python
python实现两个文件夹的同步
2019/08/29 Python
tensorflow如何批量读取图片
2019/08/29 Python
django-crontab 定时执行任务方法的实现
2019/09/06 Python
Django 权限管理(permissions)与用户组(group)详解
2020/11/30 Python
为什么使用接口?
2014/08/13 面试题
文秘专业应届生求职信范文
2013/11/14 职场文书
博士研究生自我鉴定范文
2013/12/04 职场文书
电子商务个人自荐信
2013/12/12 职场文书
2014年六一儿童节演讲稿
2014/05/23 职场文书
国庆促销活动总结
2014/08/29 职场文书
追悼会答谢词
2015/01/05 职场文书
2015年母亲节活动总结
2015/02/10 职场文书
Python Parser的用法
2021/05/12 Python