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 & ...
随机推荐
- echarts设置数据在轴线上显示
项目中遇到数据需要在右侧显示,如图,直接上代码: 1.需要在哪个轴上显示 就把那个轴写成一个数组 2.分别设置一下定位和数据即可(如下图红色部分) yAxis: [ { type: 'categor ...
- Scala实践11
1.1泛型类 泛型类是将类型作为参数的类.它们对集合类特别有用. 定义泛类型:泛型类将类型作为方括号内的参数[].一种惯例是使用字母A作为类型参数标识符,但是可以使用任何参数名称. class Sta ...
- CSS中使用文本阴影与元素阴影
文本阴影介绍 在CSS中使用text-shadow属性设置文本阴影,该属性一共有4个属性值如:水平阴影.垂直阴影.(清晰度或模糊距离).阴影颜色. text-shadow属性值说明,在文本阴影实践中: ...
- crawler碎碎念4 关于python requests、Beautiful Soup库、SQLlite的基本操作
Requests import requests from PIL import Image from io improt BytesTO import jason url = "..... ...
- java 方法定义 调用
一.定义 格式: 修饰符 返回值类型 方法名(参数){ return } 相比之下python方法的定义简单多了 public static 是修饰符 二.调用 方法名(); 注意:要在main方法中 ...
- Flask路由+视图补充
一.路由设置的两种方法 1.装饰器 @app.route('/index/') def index(): return 'Hello World!' 2.源码 route->decorator- ...
- 关于C#程序的单元测试
目录 1.单元测试概念 2.单元测试的原则 3.单元测试简单示例 4.单元测试框架特性标签 5.单元测试中的断言Assert 6.单元测试中验证预期的异常 7.单元测试中针对状态的间接测试 8.单元测 ...
- 【Java面试】java基础篇
总结个人的面试经历以及一些网上的的面试题,以供以后面试与巩固java基础. 1.String.StringBuilder和StringBuffer的区别 String用于存储不可变字符串的类,Stri ...
- 20191211 HNOI2017 模拟赛 问题A
题目: 分析: 好难好难... 下来听神仙讲.. 每一个长度为n-2的prufer序列一一对应一棵大小为n的树... 每个点在序列中的出现次数为该点的度数减一 哦??? ... 哦... prufer ...
- Python学习,第六课 - 集合
Python中集合的相关操作 集合是一个无序的,不重复的数据组合 它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 list_1 =set ...