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


Groups > comp.lang.python > #65137

Re: __init__ is the initialiser

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'explicitly': 0.05; 'differently': 0.07; 'initialize': 0.07; 'returned.': 0.07; '__init__': 0.09; 'arguments': 0.09; 'constructor': 0.09; 'derived': 0.09; 'lawrence': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'programs.': 0.14; '"python': 0.16; '__init__,': 0.16; '__new__': 0.16; 'created.': 0.16; 'expression.': 0.16; 'initialiser': 0.16; 'instance;': 0.16; "object's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'returned;': 0.16; 'runtime.': 0.16; 'semantics': 0.16; 'torn': 0.16; 'variables,': 0.16; 'wording': 0.16; 'wrote:': 0.18; 'any,': 0.19; 'header:User- Agent:1': 0.23; 'class.': 0.26; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'raised': 0.31; 'class': 0.32; 'url:python': 0.33; 'beginning': 0.33; '"the': 0.34; 'sense': 0.34; 'subject:the': 0.34; "can't": 0.35; 'classes': 0.35; 'but': 0.35; 'add': 0.35; 'c++': 0.36; 'reflect': 0.36; 'doing': 0.36; 'method': 0.36; 'useful': 0.36; 'url:org': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'called': 0.40; 'ensure': 0.60; 'most': 0.60; 'url:3': 0.61; "you're": 0.61; 'more': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'special': 0.74; '95%': 0.84; 'describes': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; 'tied': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: __init__ is the initialiser
Date Fri, 31 Jan 2014 14:52:15 -0500
References <lcgtpf$tui$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 18.189.30.229
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <lcgtpf$tui$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6217.1391197950.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391197950 news.xs4all.nl 2861 [2001:888:2000:d::a6]:58019
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65137

Show key headers only | View raw


On 1/31/14 2:33 PM, Mark Lawrence wrote:
>  From http://docs.python.org/3/reference/datamodel.html#object.__init__
> which states:-
>
> "
> Called when the instance is created. The arguments are those passed to
> the class constructor expression. 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; for example: BaseClass.__init__(self, [args...]). As a special
> constraint on constructors, no value may be returned; doing so will
> cause a TypeError to be raised at runtime.
> "
>
> Should the wording of the above be changed to clearly reflect that we
> have an initialiser here and that __new__ is the constructor?
>

I'm torn about that.  The fact is, that for 95% of the reasons you want 
to say "constructor", the thing you're describing is __init__.  Most 
classes have __init__, only very very few have __new__.

The sense that __new__ is the constructor is the one borrowed from C++ 
and Java: you don't have an instance of your type until the constructor 
has returned.  This is why __init__ is not a constructor: the self 
passed into __init__ is already an object of your class.

But that distinction isn't useful in most programs.  The thing most 
people mean by "constructor" is "the method that gets invoked right at 
the beginning of the object's lifetime, where you can add code to 
initialize it properly."  That describes __init__.

Insisting that __init__ is not a constructor makes about as much sense 
as insisting that "Python has no variables" just because they work 
differently than in C.  Python has variables, and it has constructors. 
We don't have to be tied to C++ semantics of the word "constructor" any 
more than we have to tied to its semantics of the word "variable" or "for".

Why can't we call __init__ the constructor and __new__ the allocator?

-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

Re: __init__ is the initialiser Ned Batchelder <ned@nedbatchelder.com> - 2014-01-31 14:52 -0500
  Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-01 03:42 +0000
    Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-01 15:35 +1100
      Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-01 05:51 +0000
        Re: __init__ is the initialiser Ethan Furman <ethan@stoneleaf.us> - 2014-02-01 00:28 -0800
    Re: __init__ is the initialiser Ethan Furman <ethan@stoneleaf.us> - 2014-01-31 20:55 -0800
    Re: __init__ is the initialiser Ned Batchelder <ned@nedbatchelder.com> - 2014-02-01 07:28 -0500
      Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-01 09:40 -0500
        Re: __init__ is the initialiser Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-01 15:07 +0000
          Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-01 11:17 -0500
    Re: __init__ is the initialiser Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-02 07:09 +1100
      Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-02 01:28 +0000
    Re: __init__ is the initialiser Ben Finney <ben+python@benfinney.id.au> - 2014-02-02 15:27 +1100
    Re: __init__ is the initialiser Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-03 12:38 +1300
      Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-03 00:33 +0000
        Re: __init__ is the initialiser Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-04 12:47 +1300
    Re: __init__ is the initialiser Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-03 11:02 +1100

csiph-web