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


Groups > comp.lang.python > #65150

Re: __init__ is the initialiser

Path csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!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; 'python,': 0.02; 'classes,': 0.05; 'true,': 0.05; 'differently': 0.07; 'initialize': 0.07; 'odd': 0.07; 'purpose.': 0.07; 'reason,': 0.07; 'returned.': 0.07; 'think,': 0.07; '__init__': 0.09; 'constructor': 0.09; 'enormous': 0.09; 'here?': 0.09; 'instance.': 0.09; 'lawrence': 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; 'assume': 0.14; 'programs.': 0.14; '"for"': 0.16; '__init__,': 0.16; '__new__': 0.16; 'encountered.': 0.16; 'finney': 0.16; 'fits': 0.16; 'initialiser': 0.16; 'java.': 0.16; "object's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'semantics': 0.16; 'torn': 0.16; 'underlying': 0.16; 'wording': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'slightly': 0.19; 'seems': 0.21; '>>>': 0.22; 'programming': 0.22; 'header:User- Agent:1': 0.23; 'example.': 0.24; 'precise': 0.24; 'received:comcast.net': 0.24; 'java': 0.24; 'looks': 0.24; 'developers': 0.25; 'class.': 0.26; 'define': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'serve': 0.31; 'that.': 0.31; 'commonly': 0.31; 'factor': 0.31; 'names.': 0.31; 'writes:': 0.31; 'figure': 0.32; 'languages': 0.32; 'another': 0.32; 'up.': 0.33; 'url:python': 0.33; 'beginning': 0.33; '"the': 0.34; 'sense': 0.34; 'subject:the': 0.34; "can't": 0.35; 'agree': 0.35; 'classes': 0.35; 'common': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'c++': 0.36; 'functions.': 0.36; 'keyword': 0.36; 'reflect': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'ben': 0.38; 'to:addr:python- list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'major': 0.40; 'most': 0.60; 'new': 0.61; 'url:3': 0.61; "you're": 0.61; 'name': 0.63; 'term': 0.63; 'more': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'here': 0.66; 'between': 0.67; 'natural': 0.68; '8bit%:40': 0.68; 'roles': 0.68; '8bit%:43': 0.74; 'hoping': 0.75; '8bit%:46': 0.78; '95%': 0.84; 'batchelder': 0.84; 'confusing': 0.84; 'describes': 0.84; 'different.': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; '8bit%:33': 0.91; 'mistake': 0.91; 'differences': 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 18:43:16 -0500
References <lcgtpf$tui$1@ger.gmane.org> <lcgut2$b8r$1@ger.gmane.org> <858utviwgs.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <858utviwgs.fsf@benfinney.id.au>
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.6230.1391211808.18130.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391211808 news.xs4all.nl 2916 [2001:888:2000:d::a6]:48329
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65150

Show key headers only | View raw


On 1/31/14 6:05 PM, Ben Finney wrote:
> Ned Batchelder <ned@nedbatchelder.com> writes:
>
>> On 1/31/14 2:33 PM, Mark Lawrence wrote:
>>>   From http://docs.python.org/3/reference/datamodel.html#object.__init__
>>> […]
>>> 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__.
>
> I assume the “95%” figure is made up. Whose wants are you divining, here?
>
>> Most classes have __init__, only very very few have __new__.
>
> Yes, this is true, and I agree it is a major factor in the confusion:
> the term “constructor” is commonly encountered in discussion of classes,
> and in Python, the method “__init__” is commonly encountered. It is a
> natural mistake to think Python names its constructor “__init__”, since
> most classes don't define “__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.
>
> A more salient reason, I think, is that “constructor” entails that the
> function will return the instance. This is another important reason why
> “__init__” is not a constructor: it does not return the instance.
>
>> 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__.
>
> Here we disagree. I think the meaning “… and that returns the new
> instance” is entailed in the meaning of “constructor”.
>
>> Why can't we call __init__ the constructor and __new__ the allocator?
>
> Because those terms already have meanings, and “__new__” fits the
> meaning of “constructor” better.
>

You say these terms already have meanings, and that constructor means a 
function that returns the new instance.  Then your word "constructor" 
isn't the same as C++'s word "constructor"?  Those don't return the 
instance.   Neither do Java constructors.  In terms of the code 
developers actually write in __init__, it looks an awful lot like a C++ 
or Java constructor, and serves precisely the same purpose.  The fact 
that the underlying mechanisms are slightly different seems like an odd 
reason to force a difference in names.

My point is that often the same word in two different programming 
languages won't have a precise mapping.  "Variable" is a common example. 
  Python ints are different than C ints, yet we call them both ints.  C 
and Java and Python all have "for" statements.  We didn't insist on a 
different keyword just because the semantics are different.

There are enormous differences between Python functions, C functions, 
Java functions, and Haskell functions.  But they serve similar roles in 
the programmer's toolkit, and we use the same name for them.

I'm not hoping to change any official terminology. I just think that 
calling __init__ anything other than a constructor is confusing 
pedantry.  It is a constructor, and Python constructors work differently 
than those in C++ and Java.

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

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


Thread

Re: __init__ is the initialiser Ned Batchelder <ned@nedbatchelder.com> - 2014-01-31 18:43 -0500

csiph-web