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进阶教程之循环对象
Aug 30 Python
wxPython事件驱动实例详解
Sep 28 Python
python中实现php的var_dump函数功能
Jan 21 Python
python简单实现基于SSL的IRC bot实例
Jun 15 Python
利用Python脚本实现ping百度和google的方法
Jan 24 Python
Python中pip更新和三方插件安装说明
Jul 08 Python
python文件操作之批量修改文件后缀名的方法
Aug 10 Python
python Django编写接口并用Jmeter测试的方法
Jul 31 Python
使用python写的opencv实时监测和解析二维码和条形码
Aug 14 Python
python实现简单井字棋游戏
Mar 04 Python
keras 权重保存和权重载入方式
May 21 Python
keras自动编码器实现系列之卷积自动编码器操作
Jul 03 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
3
2006/10/09 PHP
Look And Say 序列php实现代码
2011/05/22 PHP
url decode problem 解决方法
2011/12/26 PHP
php数组使用规则分析
2015/02/27 PHP
微信API接口大全
2015/04/15 PHP
jquery 的 $(&quot;#id&quot;).html() 无内容的解决方法
2010/06/07 Javascript
javascript 二进制运算技巧解析
2012/11/27 Javascript
js实现弹窗插件功能实例代码分享
2013/12/12 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
jQuery实现Twitter的自动文字补齐特效
2014/11/28 Javascript
jQuery实现的多屏图像图层切换效果实例
2015/05/07 Javascript
JavaScript获取数组最小值和最大值的方法
2015/06/09 Javascript
AngularJS基础 ng-hide 指令用法及示例代码
2016/08/01 Javascript
Angularjs中ng-repeat的简单实例
2017/08/25 Javascript
ionic3+Angular4实现接口请求及本地json文件读取示例
2017/10/11 Javascript
mui框架 页面无法滚动的解决方法(推荐)
2018/01/25 Javascript
Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方法
2018/08/09 jQuery
详解vuex中action何时完成以及如何正确调用dispatch的思考
2019/01/21 Javascript
学习RxJS之JavaScript框架Cycle.js
2019/06/17 Javascript
[07:57]2018DOTA2国际邀请赛寻真——PSG.LGD凤凰浴火
2018/08/12 DOTA
django将图片上传数据库后在前端显式的方法
2018/05/25 Python
python3 面向对象__类的内置属性与方法的实例代码
2018/11/09 Python
python处理document文档保留原样式
2019/09/23 Python
如何配置关联Python 解释器 Anaconda的教程(图解)
2020/04/30 Python
关于matplotlib-legend 位置属性 loc 使用说明
2020/05/16 Python
基于python和flask实现http接口过程解析
2020/06/15 Python
利用python实现汉诺塔游戏
2021/03/01 Python
SmartBuyGlasses台湾:名牌眼镜,名牌太阳眼镜及隐形眼镜
2017/01/04 全球购物
俄罗斯卫浴采暖及维修用品超级市场:Dkrussia
2020/05/12 全球购物
信息技术专业大学生个人的自我评价
2013/10/05 职场文书
火锅店创业计划书范文
2014/02/02 职场文书
《列夫托尔斯泰》教学反思
2014/02/10 职场文书
大学生安全责任书
2014/07/25 职场文书
小王子读书笔记
2015/06/29 职场文书
基于Redis过期事件实现订单超时取消
2021/05/08 Redis
基于Redis结合SpringBoot的秒杀案例详解
2021/10/05 Redis