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中文乱码的解决方法
Nov 04 Python
Python使用自带的ConfigParser模块读写ini配置文件
Jun 26 Python
简单学习Python多进程Multiprocessing
Aug 29 Python
Python的numpy库中将矩阵转换为列表等函数的方法
Apr 04 Python
Numpy array数据的增、删、改、查实例
Jun 04 Python
Python 3.x 判断 dict 是否包含某键值的实例讲解
Jul 06 Python
Python搭建代理IP池实现检测IP的方法
Oct 27 Python
python 视频逐帧保存为图片的完整实例
Dec 10 Python
Python使用QQ邮箱发送邮件实例与QQ邮箱设置详解
Feb 18 Python
如何在python中执行另一个py文件
Apr 30 Python
Anaconda使用IDLE的实现示例
Sep 23 Python
pandas统计重复值次数的方法实现
Feb 20 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截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
2014/06/10 PHP
PHP中的常见魔术方法功能作用及用法实例
2015/07/01 PHP
PHP设计模式之工厂方法设计模式实例分析
2018/04/25 PHP
锋利的jQuery 要点归纳(一) jQuery选择器
2010/03/21 Javascript
深入理解JavaScript中的尾调用(Tail Call)
2017/02/07 Javascript
angularjs ui-router中路由的二级嵌套
2017/03/10 Javascript
js实现图片上传预览原理分析
2017/07/13 Javascript
基于AngularJS的拖拽文件上传的实例代码
2017/07/15 Javascript
js编写简单的聊天室功能
2017/08/17 Javascript
nodejs实现百度舆情接口应用示例
2020/02/07 NodeJs
把大数据数字口语化(python与js)两种实现
2013/02/21 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
2017/01/28 Python
Python与Java间Socket通信实例代码
2017/03/06 Python
Python实现决策树C4.5算法的示例
2018/05/30 Python
python定向爬虫校园论坛帖子信息
2018/07/23 Python
对Pandas MultiIndex(多重索引)详解
2018/11/16 Python
python中的数据结构比较
2019/05/13 Python
Python使用字典实现的简单记事本功能示例
2019/08/15 Python
Python实现子类调用父类的初始化实例
2020/03/12 Python
python filecmp.dircmp实现递归比对两个目录的方法
2020/05/22 Python
python里的单引号和双引号的有什么作用
2020/06/17 Python
python实现测试工具(一)——命令行发送get请求
2020/10/19 Python
python通用数据库操作工具 pydbclib的使用简介
2020/12/21 Python
解决pip安装tensorflow中出现的no module named tensorflow.python 问题方法
2021/02/20 Python
柯基袜:Corgi Socks
2017/01/26 全球购物
印度化妆品购物网站:Nykaa
2018/07/22 全球购物
Tirendo比利时:在线购买轮胎
2018/10/22 全球购物
豪华床上用品 :Jennifer Adams
2019/09/15 全球购物
个人应聘自我评价分享
2013/11/18 职场文书
《匆匆》教学反思
2014/02/22 职场文书
教师远程培训感言
2014/03/06 职场文书
五一手机促销方案
2014/03/08 职场文书
商场开业庆典策划方案
2014/06/02 职场文书
2014年内勤工作总结
2014/11/24 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
如何利用Python实现一个论文降重工具
2021/07/09 Python