def __init__(self):

Marko Rauhamaa marko at pacujo.net
Tue Apr 26 12:38:39 EDT 2016


Steven D'Aprano <steve at pearwood.info>:

> On Tue, 26 Apr 2016 06:25 pm, Marko Rauhamaa wrote:
>> Check out some of the stdlib source code for example:
>> 
>> ========================================================================
>> class ThreadPoolExecutor(_base.Executor):
>>     def __init__(self, max_workers):
>>         """Initializes a new ThreadPoolExecutor instance.
>> 
>>         Args:
>>             max_workers: The maximum number of threads that can be used to
>>                 execute the given calls.
>>         """
>>         self._max_workers = max_workers
>>         self._work_queue = queue.Queue()
>>         self._threads = set()
>>         self._shutdown = False
>>         self._shutdown_lock = threading.Lock()
>> ========================================================================
>> 
>> Notice how _base.Executor.__init__(self) does not get called. 
>
> What makes you think it needs to be called?

I would think that's somewhat of an axiom. Whenever I derive from a
class, I consider it mandatory to delegate to its __init__ if my class
specifies an __init__ itself.

Otherwise, the underlying class must make a point to emphasize in its
API documentation that __init__ is not needed. And the underlying class
would be foolish to make such a contract because that would tie its
hands in case the class needs refactoring in the future.

Are you saying something else?

See also <URL: https://docs.python.org/3/reference/datamodel.html?highlig
ht=__init__#object.__init__>:

   If a base class has an __init__() method, the derived class’s
   __init__() method, if any, must explicitly call it to ensure proper
   initialization of the base class part of the instance

I'm saying the derived class *must not* make any conclusions on the
presence or absence of __init__ in the base class because that would be
a gross violation of encapsulation.


Marko



More information about the Python-list mailing list