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. opensuse安装Tomcat碰到的问题

    已经安装好JDE,并配置好环境变量 从官网下载Tomcat tar包,解压到用户目录,进入运行bin下的start.sh,显示运行成功,但是浏览器中输入localhost:8080连接不上 检查一番发 ...

  2. 代码注释规范-IDEA 配置 Java 类方法注释模板

    1. 引言     团队开发时,业务模块分配的越清晰,代码注释管理越完善,越有利于后面维护,后面再管理也方便不少.另外也起着"文字砖"的作用,你懂的.注释不需要很详细,把代码块方法 ...

  3. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  4. java Random类(API)

    一.过程 1.导包 2.实例化 3.使用(类的成员方法) 二.作用 生成随机数,与python中random 相似 三.常用方法 1.nextInt(),随机生成int数据类型范围的数 2.nextI ...

  5. OpenCV图像数字化

    灰度图像数字化 我们平时使用PS或者其它图像处理的软件打开一个要处理的图像,当我们将图像放大的足够大的时候我们会发现很多个灰度程度不同的小方格,其中每个方格就相当于一个像素,水平方向的方格数代表这个图 ...

  6. CQBZOJ 邮递员(直播剪枝技巧)

    题目描述 Mirko在一个山镇找到了一份邮递员的工作.这个镇可以看作一个N*N的矩形.每个区域可能是以下之一:房子K,邮政局P,草地 '.'.每个区域都有一个海拔. 每天早上,Mirko要送信给镇上所 ...

  7. 各种反弹shell方法总结

    获取shell的方法总结: shell分为两种,一种是正向shell,另一种是反向shell.如果客户端连接服务器,客户端主动连接服务器,就称为正向shell.如果客户端连接服务器,服务器想要获得客户 ...

  8. JS-09-数组

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Express+MySQL实现登录注册的demo

    MySQL5.7.20 demo准备 安装MySQL,安装完毕之后添加系统环境变量在cmd中启动服务:net start mysql57,如果是安装MySQL8.0则服务名默认时mysql80,测试安 ...

  10. 关于Windows Server 服务器 安装tomcat部署Java Web 项目的问题

    我遇到的问题是:不知道怎么配置,感觉在服务器上部署一个web项目,应该是很高大上,步骤应该很繁琐,但是,事实却不是那样.配置反而挺简单. tomcat配置:在tomcat安装目录中的conf文件夹下有 ...