Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107670

Re: def __init__(self):

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: def __init__(self):
Date 2016-04-26 19:38 +0300
Organization A noiseless patient Spider
Message-ID <87inz4uxyo.fsf@elektro.pacujo.net> (permalink)
References (1 earlier) <571F1361.3010402@digipen.edu> <85eg9sn7qo.fsf@benfinney.id.au> <mailman.100.1461656092.32212.python-list@python.org> <87lh40pyik.fsf@elektro.pacujo.net> <571f9362$0$1602$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


Steven D'Aprano <steve@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

def __init__(self): San <santanu01@gmail.com> - 2016-04-25 23:21 -0700
  Re: def __init__(self): Ben Finney <ben+python@benfinney.id.au> - 2016-04-26 16:31 +1000
  Re: def __init__(self): Gary Herron <gherron@digipen.edu> - 2016-04-26 00:06 -0700
  Re: def __init__(self): Ben Finney <ben+python@benfinney.id.au> - 2016-04-26 17:34 +1000
    Re: def __init__(self): Marko Rauhamaa <marko@pacujo.net> - 2016-04-26 11:25 +0300
      Re: def __init__(self): Steven D'Aprano <steve@pearwood.info> - 2016-04-27 02:12 +1000
        Re: def __init__(self): Random832 <random832@fastmail.com> - 2016-04-26 12:26 -0400
          Re: def __init__(self): Steven D'Aprano <steve@pearwood.info> - 2016-04-27 02:32 +1000
            Re: def __init__(self): Chris Kaynor <ckaynor@zindagigames.com> - 2016-04-26 09:59 -0700
              Re: def __init__(self): Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-27 14:54 +1000
            Re: def __init__(self): Chris Angelico <rosuav@gmail.com> - 2016-04-27 03:04 +1000
            Re: def __init__(self): Chris Kaynor <ckaynor@zindagigames.com> - 2016-04-26 10:13 -0700
            Re: def __init__(self): Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-26 11:26 -0600
            Re: def __init__(self): Chris Angelico <rosuav@gmail.com> - 2016-04-27 03:30 +1000
        Re: def __init__(self): Marko Rauhamaa <marko@pacujo.net> - 2016-04-26 19:38 +0300
  Re: def __init__(self): Random832 <random832@fastmail.com> - 2016-04-26 09:49 -0400
  Re: def __init__(self): Gary Herron <gherron@digipen.edu> - 2016-04-26 07:25 -0700

csiph-web