Python内置函数(64)——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内置函数(64)——classmethod的更多相关文章
- Python内置函数之classmethod()
函数的参数是一个函数: classmethod(func) 作用是,在外部,类对象能够直接调用类方法. 常用来作为装饰器. >>> class C: ... def f(self): ...
- Python内置函数(11)——classmethod
英文文档: classmethod(function) Return a class method for function. A class method receives the class as ...
- Python内置函数(64)——tuple
英文文档: The constructor builds a tuple whose items are the same and in the same order as iterable‘s it ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
随机推荐
- vsto下开发wps插件
我们要开发wps插件了.之前用vsto开发过word插件,我也讲过c#下如何开发wps插件(有点繁琐).如果采用c#从头再开发wps插件,那么开发出来的office加载项就会出现两个.我们要实现的wp ...
- Thinking in Java 第二章学习笔记
Java虽基于C++,但相比之下,Java是一种更加纯粹的面向对象程序设计语言. 在Java的世界里,几乎一切都是对象,而Java中的全部工作则是定义类,产生那些类的对象,以及发送消息给这些对象. 尽 ...
- systemd的作用
早上群上讨论了一下systemd的作用,还导致了一个人的直接退群,出于求知心理,搜索了一些systemd,对此也作出了一些相应的整理: 一.systemd的诞生: 学习嵌入式bootloader与ke ...
- 微信小程序-布局
flex-direction 传送门 border 传送门 边框 粗细:thin(细线).medium(中粗线)和thick(粗线) 类型:九个确定值:none(无边框线). dotted(由点组成的 ...
- 数组的迭代方法(every、filter、forEach、map、some)
every: 对数组中的,每一项运行给定函数,如果该函数对每一项都返回true,则返回true. var number = [1,2,3,4,5,6]; var result = number.eve ...
- mybatis-spring整合
1. 配置jar包 Spring,mybatis,mybatis-spring,mysql等... <properties> <project.build.sourceEncodin ...
- JQ在光标处插入文字
内容转载自网络这是一个JQ的扩展方法.在teatarea获得焦点时,往光标处插入文字,扩展代码如下 (function($){ $.fn.extend({ "insert":fun ...
- [Apio2010] 巡逻
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- 【Android】你知道还可以通过 View.animate() 来实现动画么
这次想来讲讲 View.animate(),这是一种超好用的动画实现方式,用这种方式来实现常用的动画效果非常方便,但在某些场景下会有一个坑,所以这次就来梳理一下它的原理. 基础 首先,先来看一段代码: ...
- 设计模式 --> (2)单例模式
单例模式 单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.如系统的日志输出,GUI应用必须是单鼠标 ...