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


Groups > comp.lang.python > #107645

Re: def __init__(self):

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: def __init__(self):
Date Tue, 26 Apr 2016 11:25:39 +0300
Organization A noiseless patient Spider
Lines 47
Message-ID <87lh40pyik.fsf@elektro.pacujo.net> (permalink)
References <34e51ef5-9679-40ec-bc8f-47981353e9d7@googlegroups.com> <571F1361.3010402@digipen.edu> <85eg9sn7qo.fsf@benfinney.id.au> <mailman.100.1461656092.32212.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Info mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="1534"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kfQYunsbiY1FNyuLpe8Xv"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)
Cancel-Lock sha1:8X8YsKlZngq/uWBwK9ngVvzkR8c= sha1:oJHhJKD222m5MlqDi9kwFVitNLY=
Xref csiph.com comp.lang.python:107645

Show key headers only | View raw


Ben Finney <ben+python@benfinney.id.au>:

> Gary Herron <gherron@digipen.edu> writes:
>
>>    The __init__ method is the constructor for instances of a class.
>>    It is not required, but the situations in which a constructor is
>>    not needed are few and unusual.
>
> That's needlessly confusing: ‘__init__’ is not a constructor because
> it does not construct the instance. The ‘__new__’ method is the
> constructor for a class (and returns the new instance).

I have never ever had a temptation to specify a __new__ method. I can't
imagine a *beneficial* case of overriding it.

> The ‘__init__’ method requests the already-constructed instance to
> initialise itself (and returns None).

It is a serious practical problem that an object can't guarantee that
its __init__ has been called.

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. It can
only work if _base.Executor does not specify an __init__. That's an
assumption you really couldn't make with a clear conscience even if you
were the author and maintainer of both modules
(concurrent.futures.thread and concurrent.futures._base).


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