本文源于一时好奇,想要弄清出python的staticmethod()这一builtin方法的实现,查了一些资料(主要是python官方手册了)汇集于此
python在类中,有三种调用method的方法:普通method,staticmethod和classmethod
前两个应该都好理解,classmethod就是在调用这个函数的时候,会把调用对象的class object对象隐式地传进去。咦?这个class object不是一个类型?No,在python里面,class object不像静态语言一样是个类型,它在虚拟机中,就是一个对象
普通method调用需要把自己self作为参数传递,初学的时候怎么着也不能理解,不过看多了就自然熟悉了。比较奇怪的是staticmethod和classmethod不像静态语言一样,通过保留关键字定义,而是使用@staticmethod或者staticmethod()这种builtin函数进行定义。这个@staticmethod到底是个什么东东?
@staticmethod
def foo(x):
print(x)
之前用过java,所以第一反应这是个annotation……唔,确实感觉像个AOP的东西,python里把它称作decorator。如果我们要自己实现一个staticmethod,该怎么写呢?
研究了下官方的代码,我再改了改,感觉应该这样写:
def foo(x):
print(x)
class StaticMethod(object):
def __init__(self, function):
print("__init__() called")
self.f = function
def __get__(self, instance, owner):
print("\t__get__() called")
print("\tINFO: self = %s, instance =%s, owner = %s" % (self, instance, owner))
return self.f
class Class1(object):
method = StaticMethod(foo)
if __name__ == '__main__':
ins = Class1()
print("ins = %s, Class1 = %s" % (ins, Class1))
print("ins.method = %s, Class1.method = %s" % (ins.method, Class1.method))
ins.method('abc')
Class1.method('xyz')
输出结果是:
__init__() called
ins = <__main__.Class1 object at 0xece2d0>, Class1 = <class '__main__.Class1'>
__get__() called
INFO: self = <__main__.StaticMethod object at 0xece5d0>, instance =<__main__.Class1 object at 0xece2d0>, owner = <class '__main__.Class1'>
__get__() called
INFO: self = <__main__.StaticMethod object at 0xece5d0>, instance =None, owner = <class '__main__.Class1'>
ins.method = <function foo at 0xeb6c00>, Class1.method = <function foo at 0xeb6c00>
__get__() called
INFO: self = <__main__.StaticMethod object at 0xece5d0>, instance =<__main__.Class1 object at 0xece2d0>, owner = <class '__main__.Class1'>
abc
__get__() called
INFO: self = <__main__.StaticMethod object at 0xece5d0>, instance =None, owner = <class '__main__.Class1'>
xyz
嗯,看上去一切都挺顺利,Class1包含了一个变量method,不过这个method其实也是一个特殊处理过的StaticMethod类。这个类中有一个__get__函数,当类被“get”的时候,被访问的时候,会默认把访问者的instance和class信息都传进来。所以我们看到不管是否调用method()这个函数,只要碰着了method,这个函数就会触发,就会打印出当前instance和class信息。虽然ins和Class1的instance各有不同,但__get__函数中只是返回foo函数,所以这里调用method之时就没有区别,调用的都是同一个function对象。
好的,那么classmethod又如何实现呢?
def foo2(cls, x):
print("foo2's class = ", cls)
print(x)
class ClassMethod(object):
def __init__(self, function):
print("ClassMethod: __init__() called")
self.f = function
def __get__(self, instance, owner = None):
print("\t__get__() called")
print("\tINFO: self = %s, instance =%s, owner = %s" % (self, instance, owner))
def tmpfunc(x):
print("I'm tmpfunc")
return self.f(owner, x)
return tmpfunc
class Class2(object):
method = ClassMethod(foo2)
class Class21(Class2):
pass
if __name__ == '__main__':
ins = Class2()
print("ins.method = %s, Class2.method = %s, Class21.method = %s" % (ins.method, Class2.method, Class21.method))
ins.method('abc')
Class2.method('xyz')
Class21.method('asdf')
输出结果是:
ClassMethod: __init__() called
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =<__main__.Class2 object at 0xdeb350>, owner = <class '__main__.Class2'>
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =None, owner = <class '__main__.Class2'>
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =None, owner = <class '__main__.Class21'>
ins.method = <function tmpfunc at 0xdee050>, Class2.method = <function tmpfunc at 0xdee1e8>, Class21.method = <function tmpfunc at 0xdee270>
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =<__main__.Class2 object at 0xdeb350>, owner = <class '__main__.Class2'>
I'm tmpfunc
foo2's class = <class '__main__.Class2'>
abc
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =None, owner = <class '__main__.Class2'>
I'm tmpfunc
foo2's class = <class '__main__.Class2'>
xyz
__get__() called
INFO: self = <__main__.ClassMethod object at 0xdeb250>, instance =None, owner = <class '__main__.Class21'>
I'm tmpfunc
foo2's class = <class '__main__.Class21'>
asdf
可以看出,classmethod和staticmethod的实现方法是大同小异。staticmethod比较简单,直接返回self.f变量就好了,而classmethod不行,需要把调用时候的class类型信息传给foo2函数,这个函数根据接收的class信息来作不同的工作。(不过我现在也没有想到可以用来做些什么)
有个地方值得注意,可能同志们刚才也已经想到了,我一定必须要定义一个tempfunc,再返回它才能完成工作吗?可不可以不要
def tmpfunc(x):
print("I'm tmpfunc")
return self.f(owner, x)
return tmpfunc
而直接返回一个
return self.f(owner, *args)
?
我刚试了一把,直接传args默认参数是不行的,因为__get__被调用的时候,还没有把参数传进来。只有return tmpfunc之后,Class2.method('xyz')的参数才挂在tmpfunc之上。
当然,如果有朋友成功做到了,请一定留言告诉我XD
小结:看来staticmethod和classmethod实现不是很困难,多亏了__get__函数帮忙。前文也提到__get__被调用时会把instance和class信息都填进来,真是帮了很大忙。但是,这个__get__函数到底又是怎么一回事?为什么这么神奇?这里卖个关子,本文先不讲了,下篇博文再看看这个
__get__函数吧
分享到:
相关推荐
主要介绍了python的staticmethod与classmethod实现实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
今天小编就为大家分享一篇关于Python中staticmethod和classmethod的作用与区别,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
下面小编就为大家带来一篇基于python中staticmethod和classmethod的区别(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
使用 @ 符号引用已有的函数(比如 @staticmethod、@classmethod)后,可用于修饰其他函数,装饰被修饰的函数。那么我们是否可以开发自定义的函数装饰器呢? 答案是肯定的。当程序使用“@函数”(比如函数 A)...
主要介绍了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod),结合实例形式分析了Python 类方法和实例方法及静态方法相关原理、用法及相关操作注意事项,需要的朋友可以参考下
python staticmethod and classmethod Though classmethod and staticmethod are quite similar, there's a slight difference in usage for both entities: classmethod must have a reference to a class object ...
装饰器进阶:property、staticmethod、classmethod源码分析(python代码实现) 装饰器基础 无参装饰器 ''' 假定有一个需求是:打印程序函数运行顺序 此案例打印的结果为: foo1 function is starting foo2 ...
一、装饰器decorator ... 比较常用的功能一般使用decorator来实现,例如python自带的staticmethod和classmethod。 装饰器有两种形式: 复制代码 代码如下: @A def foo(): pass 相当于: 复制代码 代码如下
3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典推导式 7 Python中单下划线和双下划线 8 字符串格式化:\x和.format 9 迭代器和生成器 10 *args and **kwargs 11 面向切面编程AOP和装饰器 12 ...
3 @staticmethod和@classmethod 。 4 类变量和实例变量 o 5 Python自省 。6字典推导式 。 7 Python中单下划线和双下划线 。8 字符串格式化:\x和.format 。 9 选代器和生成器 10*args and **kwargs 。 11 面向切面...
⾄于说没有run()没有参数self,⽽是参数cls,为什么可⽤,那就是装饰器@classmethod在起作⽤了,理解了装饰器,就知道这个问题的 答案了。不然的话,还有@staticmethod可以让⽅法连cls⽅法都不需要⼜怎么说。 ...
主要介绍了Python 静态方法和类方法,结合实例形式分析了Python类、实例、静态方法等相关概念、原理与使用技巧,需要的朋友可以参考下
1.7 比较Python(Python与其他语言的比较) 1.8 其他实现 1.9 练习 第2章 快速入门 2.1 程序输出,print语句及“Hello World!” 2.2 程序输入和raw_input()内建函数 2.3 注释 ...
少劳多得 Decorator 与 Python 之前引入的元编程抽象有着某些共同之处:即使没有这些技术,您也一样可以实现它们所提供的功能...这总是可能的,但这种功能主要是由 Python 2.2 中引入的 classmethod() 和 staticmethod
@staticmethod和@classmethod两个装饰器4. 类属性和实例属性5. Python的自省6. 列表、集合、字典推导式7. Python中单下划线和双下划线8. 格式化字符串中的%和format9. 迭代器和生成器10. args和**kwargs11. 面向切...
1.7 比较Python(Python与其他语言的比较) 1.8 其他实现 1.9 练习 第2章 快速入门 2.1 程序输出,1print语句及“HellocWorld!” 2.2 程序输入和raw_input()内建函数 2.3 注释 ...
问题,另外还引入了一些新的概念,比如 classmethod, staticmethod, super, Property 等。因此理解 descriptor 有助于更好地了解 Python 的运行机制。 那么什么是 descriptor 呢? 简而言之:descriptor 就是一类...
Python对象里有三类方法: 1 类方法 @classmethod :基于类的一些属性进行调用,第一个参数是cls 2 静态方法 @staticmethod: 本质上和普通函数没什么不同,归集到一个类下面,相当于整理 3 实例方法 :需要结合对象的...
python 中的self和cls 一句话描述:self是类(Class)实例化对象,cls就是类(或子类)本身,取决于调用的是那个类。 @staticmethod 属于静态方法装饰器,@classmethod属于类方法装饰器。我们需要从声明和使用两个...
在做面向对象编程时,我们就经常会用到 @staticmethod和 @classmethod 两个内置装饰器。此外,如果你接触过 click 模块,就更不会对装饰器感到陌生。click 最为人所称道的参数定义接口 @click.option(.