classmethod and staticmethod
classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmethod 即可以通过'类'调用 - cls.classfunc(),
也可以同通过实例调用('The instance is ignored except for its class')- instance.classfunc() / cls().classfunc()
当通过'子类'调用'基类'的 classmethod 的时候, '子类'的类对象被当做第一个参数处理.
'When a class attribute reference (for class C, say) would yield a class method object,
it is transformed into an instance method object whose __self__ attributes is C. ' 举个例子,
class A(object): @classmethod
def func(cls):
print(cls)
print('A - classmethod') class B(A):
pass if __name__ == "__main__":
A.func() #1 通过本类的'类对象'调用 classmethod
abc = A()
abc.func() #2 通过本类的'实例对象'调用 classmethod
B.func() #3 通过子类的'类对象' 调用 classmethod
bcd = B()
bcd.func() #4 通过子类的'实例对象'调用 classmethod Output,
<class '__main__.A'> #5 classmethod 的第一个参数是 '类对象'
A - classmethod
<class '__main__.A'>
A - classmethod
<class '__main__.B'> #6 通过'子类'调用'基类'的 classmethod 的时候, '子类' 的类对象被当做第一个参数处理
A - classmethod
<class '__main__.B'>
A - classmethod staticmethod 的第一个参数不在是'特殊参数'(cls 类本身, 或 self 实例), 可以将 staticmethod 理解为定义在类定义提中的普通函数.
staticmethod 提供了一个将 function objects 转换成 method objects 的方式. staticmethod 本身是不可调用的(not callable),
然而通过
staticmethod 即可以通过'类'调用 - cls.staticfunc(),
也可以同通过实例调用('The instance is ignored except for its class')- instance.staticfunc() / cls().staticfunc() 'When it would yield a static method object, it is transformed into the object wrapped by the static method object' 例子,
class A(object): @staticmethod
def func():
#print(callable(A.func))
print('A - staticmethod') class B(A):
pass if __name__ == "__main__":
A.func() #1 通过本类的'类对象'调用 staticmethod
abc = A()
abc.func() #2 通过本类的'实例对象'调用 staticmethod
B.func() #3 通过子类的'类对象' 调用 staticmethod
bcd = B()
bcd.func() #4 通过子类的'实例对象'调用 staticmethod Output,
A - staticmethod
A - staticmethod
A - staticmethod
A - staticmethod Static method objects
Static method objects provide a way of defeating the transformation of function objects to method objects.
A static method object is a wrapper around any other object, usually a user-defined method object.
When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object,
which is not subject to any further transformation. Static method objects are not themselves callable,
although the objects they wrap usually are.
Static method objects are created by the built-in staticmethod() constructor.
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 method objects
A class method object, like a static method object, is a wrapper around another object that alters the way
in which that object is retrieved from classes and class instances.
Class method objects are created by the built-in classmethod() constructor.
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. *** 注, decorator 返回的是可调用的函数或方法对象.
通常 classmethod 和 staticmethod 是通过装饰器 @classmethod 和 @staticmethod 实现的.

Classmethod and Staticmethod - Python 类方法 和 静态方法的更多相关文章

  1. python类方法和静态方法

    C++的静态方法是用static关键字,python j是没用static的. python中实现静态方法和类方法都是依赖于python的修饰器来实现的. class MyClass: def  me ...

  2. python类方法、静态方法、实例方法例子

    类方法,静态方法,普通方法 #coding=utf-8   class Foo:     def __init__(self,name):         self.name=name       d ...

  3. Python类方法、静态方法与实例方法

    静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实例化,直接在类之后使用.号运算符调用方法. 通常情况下,静态方法使用@staticmethod装饰器来声明. ...

  4. Python类方法、静态方法与实例方法 -----类里面不需要实例化参数 和没带self的函数 调用此函数的方法

    来源: https://www.cnblogs.com/blackmatrix/p/5606364.html 静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实 ...

  5. 面向对象之classmethod和staticmethod(python内置装饰器)

    对象的绑定方法复习classmethodstaticmethod TOC 对象的绑定方法复习 由对象来调用 会将对象当做第一个参数传入 若对象的绑定方法中还有其他参数,会一并传入 classmetho ...

  6. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  7. python类方法@classmethod与@staticmethod

    目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...

  8. 静态方法staticmethod和类方法classmethod

    静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...

  9. python中的静态方法和类方法

    在python中,各种方法的定义如下所示: class MyClass(object): #在类中定义普通方法,在定义普通方法的时候,必须添加self def foo(self,x): print & ...

随机推荐

  1. 【转】【e周美文】优秀博客上榜推荐

    Everybody,本周的博客推荐开始啦,记住,有好的博客可要给小活推荐一下哦. 7.19日 博客推荐 Android权限列表作者:@大漠落日 链接:http://my.eoe.cn/1103623/ ...

  2. ABA问题怎么解:AtomicStampedReference和AtomicMarkableReference

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 并发编程的基石--CAS机制这篇文章中介绍到CAS机制有 ...

  3. 生成TFRecord文件完整代码实例

    import os import json def get_annotation_dict(input_folder_path, word2number_dict): label_dict = {} ...

  4. 通过例子进阶学习C++(四)计算2的64次方,不服写写看

    ​ 本文是通过例子学习C++的第四篇,通过这个例子可以快速入门c++相关的语法. 1.乍一看题目非常简单,简单思考一下,可以通过for循环实现: #include <iostream> u ...

  5. 微信小程序---自定义三级联动

    在开发的很多电商类型的项目中,免不了会遇到三级联动选择地址信息,如果单纯的使用文本框给用户选择,用户体检可能就会差很多.今天我给大家整理了关于小程序开发利用picker-view组件和animatio ...

  6. python的break、continue、pass

    break break可以用来立即退出循环语句(包括else)continue continue可以用来跳过当次循环注意:break和continue都是只对离他最近的循环起作用 pass pass是 ...

  7. 依赖注入之IConfiguration

    public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; ...

  8. matplotlib 折线图

    1.基本要点 # 导入模块 from matplotlib import pyplot as plt # x轴数据 x = range(2, 26, 2) # y轴数据 y = [15, 13, 14 ...

  9. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  10. es7中数组如何判断元素是否存在

    const arr = [1,2,3,4,5,6] console.log(arr.includes(4)) //true