Repository navigation
Why can't I define a connection_lost_callback this way? #2017
|
In my app I subclass but this does I don't understand why in the first case the |
Replies: 2 comments 2 replies
|
This happens because That instance attribute shadows your subclass method named Use assignment after Two details matter:
If this fixes your handler, please mark the answer so others do not run into the subclass-shadowing trap. |
|
Yes, that is the distinction. In Python, methods on the class are descriptors. If there is no instance attribute with that name, But So the two equivalent safe patterns are: class MyClient(Client):
def __init__(self, url):
super().__init__(url)
self.connection_lost_callback = self._on_connection_lost
async def _on_connection_lost(self, exc):
...or assign any other async callable: client.connection_lost_callback = on_connection_lostThe important part is that |
This happens because
Client.__init__()creates an instance attribute with the same name:That instance attribute shadows your subclass method named
connection_lost_callback, so later lookup findsNone, not the method on your subclass. The reconnect path then checks/calls the instance attribute, so the subclass method is never reached.Use assignment after
super().__init__()instead:Two detail…