Why does the python class that inherits the "object" call "super" in the __init__ method?

I bumped into the source of the requests module and noticed this code:

 class Response(object): """The :class:`Response <Response>` object, which contains a server response to an HTTP request. """ def __init__(self): super(Response, self).__init__() ... more init method... 

My understanding of super() suggests that this call will do nothing at all. I found some questions about the superclass call , but all work from subclasses of other classes, not object . python docs also do not mention this construct.

It occurred to me that this might just be a mistake, and if you git blame this file in commit, which represented this line , you will see that at the time of authorship Response was a subclass of BaseResponse . Is the string just a hold against refactoring the class, or does it call super () at all?

+6
source share
2 answers

As mentioned in Corley Brigman's commentary , this is unnecessary, but harmless.

For some background, the BaseResponse class was added during Kenneth's sprint in Requests 1.0. As a result of the 1.0 code change, transport adapters were introduced that allow you to define specific behavior for some HTTP endpoints (or truly non-HTTP endpoints ). An important part of the transport adapter interface is the HTTPAdapter.build_response() method, which returns the returned original response from HTTPAdapter.send() and creates a Response request from it.

It’s clear that Kenneth saw potential utility in the form of an abstract base class for Response s, which would allow transport adapters to return Response with very different behavior to a standard HTTP Response object. For this reason, the refractor in ABC with most of the logic in the subclass seemed to make sense.

Later in the refactor, it again burst out as unnecessary complexity. The reality is that people who want to define specialized Response objects can simply subclass Response , instead of having an ABC that does nothing. This makes the main use case (vanilla queries) much cleaner in code and removes almost no utility.

When the BaseRequest class pulled out, this line was omitted, but since it does not cause any problems, it never needed to be deleted.

+2
source

This code can really do something if Response becomes part of the multiple inheritance tree. The next class on the MRO list may be something other than an object! In this case, a super call can be very necessary .

For instance,

 class NewerClass(A, Response): def __init__(self): ... do stuff ... super(NewerClass, self).__init__() 

From this class definition and without knowing what the hierarchy A is, you cannot determine what the next class will be that will call super(Response, self).__init__() . It can be an object, or it can be a base class A.

I think one problem is that the name super is confusing. It does not act as a Super Smalltalk variable unless you use only one inheritance and it does not return a superclass object. Instead, it computes the β€œnext” class to be used in accordance with the MRO order.

Classic related articles: Python Super is considered super and Python Super is considered harmful .

+3
source

Source: https://habr.com/ru/post/955613/


All Articles