JAB*_*JAB 25
看一下这个.
http://docs.python.org/howto/descriptor.html#static-methods-and-class-methods
您还可以在funcobject.c中查看类和静态方法对象的源代码:
http://hg.python.org/cpython/file/69b416cd1727/Objects/funcobject.c
类方法对象定义从第694行开始,而静态方法对象定义从第852行开始.(我确实发现,当methodobject.c也存在时,它们在funcobject.c中有标题为"method"的项目很有趣.)
作为参考,来自@JAB 答案中的第一个链接
使用非数据描述符协议,staticmethod() 的纯 Python 版本将如下所示:
Run Code Online (Sandbox Code Playgroud)class StaticMethod(object): "Emulate PyStaticMethod_Type() in Objects/funcobject.c" def __init__(self, f): self.f = f def __get__(self, obj, objtype=None): return self.f
...
使用非数据描述符协议,classmethod() 的纯 Python 版本将如下所示:
Run Code Online (Sandbox Code Playgroud)class ClassMethod(object): "Emulate PyClassMethod_Type() in Objects/funcobject.c" def __init__(self, f): self.f = f def __get__(self, obj, klass=None): if klass is None: klass = type(obj) def newfunc(*args): return self.f(klass, *args) return newfunc
| 归档时间: |
|
| 查看次数: |
6743 次 |
| 最近记录: |