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一共为 ...
随机推荐
- vue笔记-列表渲染
用v-for把一个数组对应为一组元素 使用方法:v-for="(item,index) in items"//也可以使用of替代in { items:源数组 item:数组元素迭代 ...
- [sublime]用sublime搭建属于自己的Python的IDE
先在sublime中利用package control下载anaconda插件, 然后更改python的路径,不知道自己anaconda的python在哪,可以在prompt用命令where pyth ...
- 在NSMutableArray中添加空元素:NSNull类的使用
有时需要将一些表示“空”的对象添加到array中.NSNull类正是基于这样的目的产生的.用NSNull表示一个占位符时,语句表达如下: [array addObject:[NSNull null]] ...
- time-based基于google key生成6位验证码(google authenticator)
由于公司服务器启用了双因子认证,登录时需要再次输入谷歌身份验证器生成的验证码.而生成验证码是基于固定的算法的,以当前时间为基础,基于每个人的google key去生成一个6位的验证码.也就是说,只要是 ...
- 记一次非常规方法对接硬件设备(Grason Stadler GSI 61)
Grason Stadler GSI 61 电测听设备 (写下设备的名字, 希望别人遇坑可以搜到) 对接说明 设备厂家提供自带的软件,但是没有找到接口说明.我们需要获取设备发送过来的数据. 厂家提供的 ...
- webpack4 打包报错 :regeneratorRuntime is not defined
使用async函数,在webpack打包时报错 babel-polyfill is required. You must also install it in order to get async/a ...
- ios中getTime()的兼容性问题
· var getTime = function(time){ var myDate = new Date(time); var u = navigator.userAgent; var isAndr ...
- Kubernetes中的RBAC
Kubernetes中,授权有ABAC(基于属性的访问控制).RBAC(基于角色的访问控制).Webhook.Node.AlwaysDeny(一直拒绝)和AlwaysAllow(一直允许)这6种模式. ...
- poi导出Excel直接在浏览器下载
需求:导出成Excel格式,要在浏览器看到类似于下载的效果. 导出的Excel和下载在同一个目录下. xxController.java // 导出 @RequestMapping(value = & ...
- NFS 系统搭建 - 成功
NFS是Network File System的缩写,即文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. 工作流程 1.由程序在NFS客户端发起存取文件的请求,客户端本地 ...