You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The attribute lookup super(B, obj).m searches obj.class.mro for the base class A immediately following B and then returns A.dict['m'].get(obj, B). If not a descriptor, m is returned unchanged. If not in the dictionary, m reverts to a search using object.getattribute().
It seems that at least in some cases this leads to counterintuitive behaviour, and falling back to A.__dict__['m'].__get__(obj, A) (note the change in owner from B to A) seems preferable. Is that a particular case for pyfields because we have a descriptor for the __init__ method and therefore would like it to "stay" on A once called, or would this be preferable in all cases ?
classAInitDescriptor(object):
def__get__(self, instance, owner):
print("called on instance %s - creating init for owner class %s"% (instance, owner.__name__))
def__init__():
pass# Assign the __init__ method to the classifowner.__name__!='A':
raiseValueError("This is the __init__ descriptor for A, not for %s!"%owner.__name__)
owner.__init__=__init__# finally returnreturn__init__classA(object):
__init__=AInitDescriptor()
classB(A):
def__init__(self):
super(B, self).__init__()
# this leads to an errorb=B()
Note that trivial workarounds for this specific example can be found, but the general question remains interesting to discuss.
smarie
changed the title
[pyfields] possible bug with super() in combination with descriptor
Possible bug with super() in combination with descriptor
Sep 7, 2020
From the documentation
It seems that at least in some cases this leads to counterintuitive behaviour, and falling back to
A.__dict__['m'].__get__(obj, A)
(note the change in owner from B to A) seems preferable. Is that a particular case forpyfields
because we have a descriptor for the__init__
method and therefore would like it to "stay" on A once called, or would this be preferable in all cases ?Note that trivial workarounds for this specific example can be found, but the general question remains interesting to discuss.
See smarie/python-pyfields#53 (comment)
The text was updated successfully, but these errors were encountered: