Marco
Marco

Reputation: 1493

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

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.

Upvotes: 2

Views: 4215

Answers (1)

Dunes
Dunes

Reputation: 40713

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__'

Upvotes: 3

Related Questions