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. Spring Boot2 系列教程 (十) | 实现声明式事务

    前言 如题,今天介绍 SpringBoot 的 声明式事务. Spring 的事务机制 所有的数据访问技术都有事务处理机制,这些技术提供了 API 用于开启事务.提交事务来完成数据操作,或者在发生错误 ...

  2. npm 安装出现 run `npm audit fix` to fix them, or `npm audit` for details 解决办法

    1.npm  audit fix 2. npm audit fix --force 3.npm audit 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链 ...

  3. CF - 高精度 + 贪心

    Last year Bob earned by selling memory sticks. During each of n days of his work one of the two foll ...

  4. sql if else 语句

    IF ELSE 语句IF ELSE 是最基本的编程语句结构之一几乎每一种编程语言都支持这种结构而它在用于对从数据库返回的数据进行检查是非常有用的TRANSACT-SQL 使用IF ELSE的例子如下语 ...

  5. <a>标签的href和onclick属性【转】

    1链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 2假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行, ...

  6. Spring MVC中的拦截器Interceptor

    谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...

  7. 51Nod 1238 最小公倍数之和V3

    题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...

  8. 创建dynamics CRM client-side (七) - 用JS 来控制Auto-Save

    在我们的system setting里面, 我们可以设置打开/关闭 auto save的功能. 我们可以用js来控制auto-save this.formOnSave = function (exec ...

  9. AMD R5 2400G插帧教程

    最近买的小主机带的是AMD R5 2400G显卡,支持AMD的插帧技术,Sandeepin肯定要体验一把效果. BlueskyFRC 按照网上的教程配置,似乎2400G显卡驱动里没有AMD Fluid ...

  10. 异想家Win10系统安装的软件与配置

    1.C盘推荐一个硬盘,256G,安装好驱动,显卡配置好高性能,激活Win10,屏蔽WIn10驱动更新(Show or hide updates.diagcab),改电脑名称为Sandeepin-PC. ...