Classmethod and Staticmethod - Python 类方法 和 静态方法
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 类方法 和 静态方法的更多相关文章
- python类方法和静态方法
C++的静态方法是用static关键字,python j是没用static的. python中实现静态方法和类方法都是依赖于python的修饰器来实现的. class MyClass: def me ...
- python类方法、静态方法、实例方法例子
类方法,静态方法,普通方法 #coding=utf-8 class Foo: def __init__(self,name): self.name=name d ...
- Python类方法、静态方法与实例方法
静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实例化,直接在类之后使用.号运算符调用方法. 通常情况下,静态方法使用@staticmethod装饰器来声明. ...
- Python类方法、静态方法与实例方法 -----类里面不需要实例化参数 和没带self的函数 调用此函数的方法
来源: https://www.cnblogs.com/blackmatrix/p/5606364.html 静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实 ...
- 面向对象之classmethod和staticmethod(python内置装饰器)
对象的绑定方法复习classmethodstaticmethod TOC 对象的绑定方法复习 由对象来调用 会将对象当做第一个参数传入 若对象的绑定方法中还有其他参数,会一并传入 classmetho ...
- python @classmethod和@staticmethod区别
python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- 静态方法staticmethod和类方法classmethod
静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...
- python中的静态方法和类方法
在python中,各种方法的定义如下所示: class MyClass(object): #在类中定义普通方法,在定义普通方法的时候,必须添加self def foo(self,x): print & ...
随机推荐
- Download EditPlus Text Editor
突然发现EditPlus还是很强大的,很好用,破解也很方便,有个牛人做了在线生成验证码,只能说服!! 下边把官网的最新下载地址贴出,当然还有在线生成验证码喽. EditPlus Text Editor ...
- 程序员如何才能跨过高级级别,譬如腾讯T3.1/阿里P7
首先自我介绍下自己履历:5年前过了腾讯的T3.2,最近又在1年多前过了阿里的P8,目前在B站. **腾讯** 在腾讯我是T2.1社招一般水平入职的,3年后到了T3.2.中间是经历过几个转变:刚来的半年 ...
- 通过核心API启动单个或多个scrapy爬虫
1. 可以使用API从脚本运行Scrapy,而不是运行Scrapy的典型方法scrapy crawl:Scrapy是基于Twisted异步网络库构建的,因此需要在Twisted容器内运行它,可以通过两 ...
- 枚举 + exgcd
题意:已知xi=(a*xi-1+b) mod 10001,且告诉你x1,x3.........x2*t-1,让你求出其偶数列 思路分析 : 题目所要求的的是对 10001 取余,由模运算的性质可知,a ...
- .NET为什么要使用异步(async)编程?⭐⭐⭐⭐⭐
.NET为什么要使用异步(async)编程? 有几点坐下笔记 ⭐⭐⭐⭐: 1. 同步方法 static void Main(string[] args) { Console.WriteLine($&q ...
- 读取配置文件,appsettings.json和注入ICO
https://www.cnblogs.com/knowledgesea/p/7079880.html 引入Nuget的两个类库 Microsoft.Extensions.Configuration ...
- Java动态编译技术原理
这里介绍Java动态编译技术原理! 编译,一般来说就是将源代码转换成机器码的过程,比如在C语言中中,将C语言源代码编译成a.out,,但是在Java中的理解可能有点不同,编译指的是将java 源代码转 ...
- 研究僧丨Window实用利器分享
本人CS在读小硕,平时工作环境主要是win10加ubuntu,下面推荐一些我用过且觉得不错的应用. PS:我列举的应用基本被下面的网站收录,大家不妨去里面淘淘看. Windows 绝妙项目 Aweso ...
- Activiti 手工任务(manualTask)
Activiti 手工任务(manualTask) 作者:Jesai 前言: 手工任务就是一个自动执行的过程.手动任务几乎不在程序中做什么事情,只是在流程的历史中留下一点痕迹,表明流程是走过某些节点的 ...
- python 面向对象-初识
一.分类 1.面向过程 2.面向函数 3.面向对象 二.类和对象 1.宏关 类是抽象的,对象是具体的 2.实例化 类->对象 3.类的作用 1)实例化成对象 实例化的过程,创建self对象,调用 ...