我正在尝试编写一个描述符来调用cls.method和obj.method上的不同函数。
以下工作但继承中断
class dynamicmethod:
def __init__(self, f=None, m=None):
self.f = f
self.m = m
def __get__(self, obj, objtype=None):
if obj is not None and self.f is not None:
return types.MethodType(self.f, obj)
elif objtype is not None and self.m is not None:
return types.MethodType(self.m, objtype)
else:
raise AttributeError('No associated method')
def method(self, f):
return type(self)(f, self.m)
def classmethod(self, m):
return type(self)(self.f, m)
class A:
@dynamicmethod
def a(self):
print('Called from obj {} defined in A'.format(self))
@a.classmethod
def a(cls)
print('Called from class {} defined in A'.format(cls))
# so far everything works
class B(A):
@A.a.method
def a(self):
print('Called from obj {} defined in B'.format(self))
#AttributeError: 'function' object has no attribute method
#Same happens for A.a.classmethod
比较getter / setter / deleter可以从属性对象中调用,可以继承类我做错了什么?
编辑:更新示例
答案 0 :(得分:0)
所以我发现了这个问题。问题是@A.a.method
正在调用__get__
这些属性通过在self
时返回obj is None
来避免此问题,但是,我显然无法使用该技巧,因为我想在obj is None
时调度到classmethod。< / p>
解决方案是使用元类将dynamicmethod对象引入继承类的范围
class dynamicmethod:
'''
Descriptor to allow dynamic dispatch on calls to class.Method vs obj.Method
fragile when used with inheritence, to inherit and then overwrite or extend
a dynamicmethod class must have dynamicmethod_meta as its metaclass
'''
def __init__(self, f=None, m=None):
self.f = f
self.m = m
def __get__(self, obj, objtype=None):
if obj is not None and self.f is not None:
return types.MethodType(self.f, obj)
elif objtype is not None and self.m is not None:
return types.MethodType(self.m, objtype)
else:
raise AttributeError('No associated method')
def method(self, f):
return type(self)(f, self.m)
def classmethod(self, m):
return type(self)(self.f, m)
def make_dynamicmethod_meta(meta):
class _dynamicmethod_meta(meta):
def __prepare__(name, bases, **kwargs):
d = meta.__prepare__(name, bases, **kwargs)
for base in bases:
for k,v in base.__dict__.items():
if isinstance(v, dynamicmethod):
if k in d:
raise ValueError('Multiple base classes define the same dynamicmethod')
d[k] = v
return d
return _dynamicmethod_meta
dynamicmethod_meta=make_dynamicmethod_meta(type)
class A(metaclass=dynamicmethod_meta):
@dynamicmethod
def a(self):
print('Called from obj {} defined in A'.format(self))
@a.classmethod
def a(cls)
print('Called from class {} defined in A'.format(cls))
class B(A):
@a.method
def a(self):
print('Called from obj {} defined in B'.format(self))
A.a()
A().a()
B.a()
B().a()
结果:
Called from class <class 'A'> defined in A
Called from obj <A object at 0x7faef7cde7f0> defined in A
Called from class <class 'B'> defined in A
Called from obj <B object at 0x7faef7cde7f0> defined in B
编辑:为避免“使用”元类,请将元类添加到示例