获取绑定内置函数的本地方法实例
Obtain a native method instance that binds a built-in function
Python 数据模型的 partially documented 行为是 __getattribute__
不绑定“内置”函数:
import logging, numpy
class TestBind:
nobind = print
nobind2 = numpy.array
binds = logging.error
def binds2(self): pass
print(TestBind.nobind)
print(TestBind.nobind2)
print(TestBind.binds)
print(TestBind.binds2)
# <built-in function print>
# <built-in function array>
# <function error at 0x1feedbeed>
# <function TestBind.binds2 at 0x1feedbeef>
t = TestBind()
print(t.nobind)
print(t.nobind2)
print(t.binds)
print(t.binds2)
# <built-in function print>
# <built-in function array>
# <bound method error of <__main__.TestBind object at 0x1beefcafe>>
# <bound method TestBind.binds2 of <__main__.TestBind object at 0x1beefcafe>>
print(type(t.binds))
# method
没有等同于classmethod
/staticmethod
的内置“instancemethod”描述符。可以定义一个,例如
from functools import partial
class instancemethod:
__slots__ = '_func',
def __init__(self, func):
self._func = func
def __get__(self, inst, cls):
return self._func if inst is None else partial(self._func, inst)
class TestBind:
ibinds = instancemethod(logging.error)
...
但结果自然不是 method
对象,并且缺少绑定方法的属性:
print(t.ibinds)
# functools.partial(<function error at 0x1feedbeed>, <__main__.TestBind object at 0x1051af310>)
t.ibinds.__self__ # AttributeError
t.ibinds.__name__ # AttributeError
t.ibinds.__func__ # AttributeError
t.ibinds.__doc__ # wrong doc
是否可以编写一些 instancemethod
描述符(或其他任何东西)来为 C 定义的函数生成绑定 method
实例?
分辨率:
根据莫妮卡下面我使用的回答
from types import MethodType
class instancemethod:
"""
Convert a function to be an instance method.
"""
__slots__ = '_func',
def __init__(self, func):
self._func = func
def __get__(self, inst, owner=None):
return self._func if inst is None else MethodType(self._func, inst)
可以直接构造方法对象types.MethodType
:
import types
class instancemethod:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner=None):
if instance is None:
return self.func
return types.MethodType(self.func, instance)
请注意,types.MethodType
的签名可能会更改。它在 Python 2 中曾经不同,当时未绑定的方法对象仍然存在。
Python 数据模型的 partially documented 行为是 __getattribute__
不绑定“内置”函数:
import logging, numpy
class TestBind:
nobind = print
nobind2 = numpy.array
binds = logging.error
def binds2(self): pass
print(TestBind.nobind)
print(TestBind.nobind2)
print(TestBind.binds)
print(TestBind.binds2)
# <built-in function print>
# <built-in function array>
# <function error at 0x1feedbeed>
# <function TestBind.binds2 at 0x1feedbeef>
t = TestBind()
print(t.nobind)
print(t.nobind2)
print(t.binds)
print(t.binds2)
# <built-in function print>
# <built-in function array>
# <bound method error of <__main__.TestBind object at 0x1beefcafe>>
# <bound method TestBind.binds2 of <__main__.TestBind object at 0x1beefcafe>>
print(type(t.binds))
# method
没有等同于classmethod
/staticmethod
的内置“instancemethod”描述符。可以定义一个,例如
from functools import partial
class instancemethod:
__slots__ = '_func',
def __init__(self, func):
self._func = func
def __get__(self, inst, cls):
return self._func if inst is None else partial(self._func, inst)
class TestBind:
ibinds = instancemethod(logging.error)
...
但结果自然不是 method
对象,并且缺少绑定方法的属性:
print(t.ibinds)
# functools.partial(<function error at 0x1feedbeed>, <__main__.TestBind object at 0x1051af310>)
t.ibinds.__self__ # AttributeError
t.ibinds.__name__ # AttributeError
t.ibinds.__func__ # AttributeError
t.ibinds.__doc__ # wrong doc
是否可以编写一些 instancemethod
描述符(或其他任何东西)来为 C 定义的函数生成绑定 method
实例?
分辨率:
根据莫妮卡下面我使用的回答
from types import MethodType
class instancemethod:
"""
Convert a function to be an instance method.
"""
__slots__ = '_func',
def __init__(self, func):
self._func = func
def __get__(self, inst, owner=None):
return self._func if inst is None else MethodType(self._func, inst)
可以直接构造方法对象types.MethodType
:
import types
class instancemethod:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner=None):
if instance is None:
return self.func
return types.MethodType(self.func, instance)
请注意,types.MethodType
的签名可能会更改。它在 Python 2 中曾经不同,当时未绑定的方法对象仍然存在。