python内置函数2-classmethod()

Help on class classmethod in module __builtin__:

class classmethod(object)

|  classmethod(function) -> method

|

|  Convert a function to be a class method.

|

|  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:

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

|        f = classmethod(f)

|

|  It can be called either on the class (e.g. C.f()) or on an instance

|  (e.g. 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 the staticmethod builtin.

|

|  Methods defined here:

|

|  __get__(...)

|      descr.__get__(obj[, type]) -> value

|

|  __getattribute__(...)

|      x.__getattribute__(‘name‘) <==> x.name

|

|  __init__(...)

|      x.__init__(...) initializes x; see help(type(x)) for signature

|

|  ----------------------------------------------------------------------

|  Data descriptors defined here:

|

|  __func__

|

|  ----------------------------------------------------------------------

|  Data and other attributes defined here:

|

|  __new__ = <built-in method __new__ of type object>

|      T.__new__(S, ...) -> a new object with type S, a subtype of T

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(object):

@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.

Changed in version 2.4: Function decorator syntax added.

中文说明:

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

class C:

@classmethod

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

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

>>> 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 "<stdin>", line 1, in <module>

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

时间: 2024-10-05 22:28:26

python内置函数2-classmethod()的相关文章

Python内置函数之classmethod()

函数的参数是一个函数: classmethod(func) 作用是,在外部,类对象能够直接调用类方法. 常用来作为装饰器. >>> class C: ... def f(self): ... print('ok') ... >>> C.f() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() missing

Python内置函数(60)——classmethod

英文文档: staticmethod(function) Return a static method for function. A static method does not receive an implicit first argument. The @staticmethod form is a function decorator – see the description of function definitions in Function definitions for de

Python补充--Python内置函数清单

Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数:基本数据类型 type()反过头来看看 dir()   help()    len()词典 len()文本文件的输入输出 open()循环设计 range()   enumerate()    zip()循环对象

python学习系列--python内置函数(一)

先列出所有的python内置函数,可以看到还是挺多的. abs()        求给定数的绝对值. all()          传入一个列表,只有当列表中所有元素都是真时,该函数返回真. any()        传入一个列表,只要列表中有一个元素为真,该函数即返回真. ascii()       执行对象中的__repr__方法.该函数在python2.7中已弃用. bin()         将给定的值转换成二进制. bool()       判断真假. bytearray()     

Python内置函数_数学运算类

本文和大家分享的主要是python内置函数数据运算类相关内容,一起来看看吧,希望对大家学习python 有所帮助. abs abs(x) 求绝对值 · X可以是整型,也可以是复数 · 若X是复数,则返回复数的模 >>> abs(-1)1>>> abs(-3+4j)5.0>>> bin bin(x) 将整数x转换为二进制字符串 >>> bin(2)'0b10'>>> bin(3)'0b11' bool bool([x]

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a

python基础9 -----python内置函数

python内置函数1 一.python内所有的内置函数: 二.python内常用的内置函数: 三.python内内置函数详解: 1.数学运算函数: 2.集合类函数: 3.逻辑类函数: 4.映射类函数: 5.IO操作:

Python 内置函数sorted()有哪些高级用法?

本文和大家分享的主要是python内置函数sorted()的相关内容,一起来看看吧,希望对大家学习python http://www.maiziedu.com/land/python/有所帮助. 1.对于 Python 内置函数 sorted() ,先拿来跟list(列表)中的成员函数 list.sort() 进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. 2.主要的区别在于, list.sort() 是对已经存在的列表进行操作,进而可以改变

[python基础知识]python内置函数map/reduce/filter

python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法)来迭代遍历每个序列中的元素. 返回bool_func结果为true的元素的序列(注意弄清楚序列是什么意思)http://blog.csdn.net/bolike/article/details/19997465序列参考</a> 如果filter参数值为None,list参数中所有为假的元 素都将被

lambda 表达式+python内置函数

#函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda python内置函数 abc 获取绝对值 all 循环对象,都为真就返回真否则为假 >>> li[1, 2, 3]>>> all(li)True #0为假 >>> li=[0,1,2,3]>>> all(li)False bool 真假判断