classmethod(function)

中文说明:

classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:class C:

@classmethod

def f(cls, arg1, arg2, ...): ...

类方法既可以直接类调用(C.f()),也可以进行实例调用(C().f())。

版本:python2.2中新增,在python2.4中增加新功能。python3中仍可用。

英文说明:

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.

For more information on class methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

New in version 2.2.

Changed in version 2.4: Function decorator syntax added.

代码实例:>>> class C:

... @classmethod

... def f(self):

... print "This is a class method"

...

>>> C.f()

This is a class method

>>> c = C()

>>> c.f()

This is a class method

>>> class D:

... def f(self):

... print " This is not a class method "

...

>>> D.f()

Traceback (most recent call last):

File "", line 1, in

TypeError: unbound method f() must be called with D instance as first argument (got nothing instead)

>>> d = D()

>>> d.f()

This is not a class method

class函数 python_python函数之classmethod()相关推荐

  1. fun函数python_Python函数

    Python函数 一般函数定义方式 def sum(a, b): return a+b ##返回a与b的和1 2 3 一些概念与特性 嵌套 Python的函数是可以嵌套定义的 def Fun1(): ...

  2. 闭包函数 python_python 函数,闭包

    假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return m bigger = my_max(10,20) print( ...

  3. 分割函数python_python strip() 函数和 split() 函数的详解及实例

    一直以来都分不清楚strip和split的功能,实际上strip是删除的意思:而split则是分割的意思.因此也表示了这两个功能是完全不一样的,strip可以删除字符串的某些字符,而split则是根据 ...

  4. 理解函数的相关概念python_Python函数的概念和使用

    函数 为了便于程序的维护和更好的实现模块化,好的程序都会分解为很多函数. 可以这么说,对于任何的编程语言,函数都是一个非常重要的概念. python 不仅简化了函数的定义过程,而且还大量借鉴了其他函数 ...

  5. 文件函数python_Python 基础之文件 函数

    今天回顾一下之前学的文件操作相关知识点,对于文件的操作,主要有一下几部分构成: 一.文件的基础知识 1.文件操作的基本流程 文件操作其实可以分成三大部分: 1.打开文件,获取文件句柄并赋予一个变量 2 ...

  6. def func python_python函数之基础

    一: 函数的定义与调用 1.1 :函数的定义 def 关键字必需写 函数名必需是字母,数字,下划线组合,并且不能以数字开头 函数名后面要加括号然后":" 为函数写注释是一个好习惯 ...

  7. update函数python_python update函数

    在前一篇文章  python ChainMap中我们介绍了关于python内置函数 ChainMap的使用,ChainMap函数和update函数类似,都是对字典操作,也是将多个字典合并,那么问题来了 ...

  8. python定义匿名函数关键字_Python(11):Python函数基础(定义函数、函数参数、匿名函数)...

     函数先定义函数,后调用 一.定义函数: 1.简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数. ...

  9. 内置函数 ,匿名函数

    python之内置函数,匿名函数 内置函数 我们一起来看看python里的内置函数.什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本 ...

最新文章

  1. webpack+vue+vueRouter模块化构建完整项目实例详细步骤-入门篇
  2. HDU 1325 Is It A Tree?(并查集)
  3. C#中is、as以及强制转换之间区别
  4. 气象研究中的大气稳定性 Atmosphere stability
  5. [Java Framework] SpringBoot几种启动后自动初始化的几种方式
  6. 省市名_code_拼音
  7. Win10怎么看内存条频率
  8. 使用刻录机时的注意事项
  9. PHP 面试总结(持续更新) --小丑
  10. TCmalloc (google开源项目核心部分模拟实现)
  11. 什么是FOUC?如何避免FOUC?
  12. Normalization如何解决梯度消失
  13. 大数据量查询解决方案
  14. java协议标准与规范
  15. 程序员如何接私活、外包的秘技
  16. 夏洛克之眼_如果他是开发人员,我感觉就像夏洛克一样
  17. 均匀”的本质------贝特朗悖论的思考
  18. BUUCTF-Pwn-jarvisoj_level2
  19. win2012 磁盘阵列 介绍和实操
  20. python对数函数如何表示

热门文章

  1. dijkstra算法matlab程序_编程习题课 | 用最短路算法为你的小地图导航
  2. 如何将本地项目发布到gitee?
  3. combox qt 引起的删除失败_关于QT的QCombox的掉坑出坑
  4. c++new时赋初值_基于ssm的客户管理系统 - C_you_again
  5. 如何在Eclipse中添加新建包,java文件,工程工具栏按钮
  6. oracle bi publisher 安装,Oracle BI Publisher 企业版安装后的配置(BI Publisher Enterpr
  7. MySQL的datetime日期格式化,和Oracle的datetime日期格式化
  8. Java+Selenium3.3.1环境搭建
  9. 腾讯后台开发面试题--整理1
  10. 网络编程学习笔记--1.socket可读可写条件