TypeError: descriptor '__weakref__' doesn't apply to object from parent __str__ method

社会主义新天地 提交于 2019-12-24 19:26:18

问题


I have a parent class, let's call it A and a bunch of child classes, B, C, D, etc. I want to define a single __str__() method on the parent class and inside it I want to access the members of the child class via dir(self). It works, but when I check if the name is a callable with callable(getattr(self, attr)) I get:

TypeError: descriptor '__weakref__' for 'A' objects doesn't apply to 'B' object

The code that raises the error is this:

[attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]

Can someone explain what does it mean?

Sidenote: I managed to get all class members cycling through self.__dict__, my question is just out of curiosity, since I cannot understand the error.

EDIT

I made a simpler version of the code:

class A():

    def __init__(self):
        pass

    def test(self, par):
        print('10')

    def __str__(self):
        members = [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]

        str = ''
        for m in members:
            str += m
            str += ','

        return str

class B(A):

    def test_b(self, par):
        print('10_b')

    def __init__(self,first,second):
        self.first = first
        self.second = second

if __name__ == '__main__':   
    b = B('one', 'two')
    print(b)

But here the code works and correctly prints first,second,

The original code is packaged, with the equivalent of A and B in a separate package that is called by a "main" script. The same code in the __str__() method of the class in the actual code raises the error.


回答1:


There is something very funky is going on with your code. You'll need to give a lot more detail if there's any chance of finding the problem. The error you reference is raised in one place only (here) -- in a method that protects against descriptors from one class (like __weakref__) from being used on another incompatible type.

The only way I could reproduce the error (and have B subclass A) was like this:

class A:
    pass

class Z:
    pass

class B(A,Z):
    pass

B.__weakref__.__get__(A()) # works
B.__weakref__.__get__(Z()) # fails

# because
assert B.__weakref__ == A.__weakref__ # is true
assert B.__weakref__ == Z.__weakref__, 'B uses A.__weakref__ rather than  Z.__weakref__'


来源:https://stackoverflow.com/questions/45481440/typeerror-descriptor-weakref-doesnt-apply-to-object-from-parent-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!