Python内置函数(11)——classmethod
英文文档:
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内置函数(11)——classmethod的更多相关文章
- Python内置函数之classmethod()
函数的参数是一个函数: classmethod(func) 作用是,在外部,类对象能够直接调用类方法. 常用来作为装饰器. >>> class C: ... def f(self): ...
- Python内置函数(64)——classmethod
英文文档: classmethod(function) Return a class method for function. A class method receives the class as ...
- Python内置函数(11)——complex
英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
随机推荐
- [linux]使用curl进行GET、POST和网页调试
以前在win下开发的时候,习惯用的调试工具是谷歌自带控制台和postman,谷歌控制台比较方便,但不能对请求进行重发,post虽然很好用,但是感觉还是有点'大'.在linux下,其实完全没有那么麻烦, ...
- AutoProject Studio 自动化项目生成器 下载地址
一.ZCN.NET 自动化项目生成器 下载专区 [如果下载链接不可用,请直接联系作者,QQ:3080400049] 1.1.ZCN.NET 自动化项目生成器 安装程序 V2016Beta1 ...
- 记一次线上Zabbix对Redis监控实录
前言:Redis作为缓存服务器我想大家都比较的熟悉,那么,如果想要更好的维护和监控,那么我们会对其redis服务器统一监控起来,如何监控呢?如果在生产环境一台服务器部署多个redis,这样就会出现多个 ...
- Django----博客文章数据返回
步骤1:新建视图函数 from django.shortcuts import render from django.http import HttpResponse; from blog.model ...
- Linux中的官方源、镜像源汇总
转载一篇文章,很有用 (一).企业站 搜狐: http://mirrors.sohu.com/ 网易: http://mirrors.163.com/ 阿里云: http://mirrors.aliy ...
- Game 23
Polycarp plays "Game 23". Initially he has a number nn and his goal is to transform it to ...
- spring-boot-starter-actouator2.1.4与c3p0版本0.9.1.2冲突
报错前的pom文件: <?xml version="1.0" encoding="UTF-8"?><project xmlns="h ...
- 从Facebook数据泄露事件看大数据时代的个人信息安全问题
进入21世纪后,互联网开始大规模普及,线上业务和线上服务也开始逐渐走入人们的生活.尤其在智能手机和移动互联网诞生以后,人们对网络的依赖更是与日俱增.然而,伴随而来的则是涉及个人隐私的信息安全问题.个人 ...
- ubuntu的安装及ubuntu中安装mysql和tomcat
一.安装ubuntu 1.创建虚拟机 2.向导选择自定义 3.然后下一步再下一步,直到这里,稍后再安装系统 4.然后选择linux,注意这里下面的下拉选择Ubuntu64,因为我们下载的是64位的,如 ...
- python批量提取eml附件
从批量eml文件中提取附件,使用方式如下 代码如下 import email import os import sys #获取eml附件信息 def Get_Annex_Message(FilePat ...