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


Groups > comp.lang.python > #65148

Re: __init__ is the initialiser

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.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.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'true,': 0.05; 'initialize': 0.07; 'reason,': 0.07; 'returned.': 0.07; 'think,': 0.07; '__init__': 0.09; 'constructor': 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; '__init__,': 0.16; '__new__': 0.16; 'encountered.': 0.16; 'finney': 0.16; 'fits': 0.16; 'initialiser': 0.16; "object's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'torn': 0.16; 'wording': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'header:User-Agent:1': 0.23; 'class.': 0.26; 'define': 0.26; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'commonly': 0.31; 'factor': 0.31; 'writes:': 0.31; 'figure': 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; 'but': 0.35; 'add': 0.35; 'c++': 0.36; 'received:com.au': 0.36; 'reflect': 0.36; 'method': 0.36; 'useful': 0.36; 'url:org': 0.36; 'should': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; '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; 'term': 0.63; 'more': 0.64; 'skip:\xe2 10': 0.65; 'world': 0.66; 'here': 0.66; 'natural': 0.68; '8bit%:40': 0.68; 'money': 0.72; '8bit%:43': 0.74; '8bit%:46': 0.78; '95%': 0.84; 'alone.': 0.84; 'batchelder': 0.84; 'describes': 0.84; 'received:125': 0.84; 'sweat': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; '\xe2\x80\x9cthis': 0.84; '8bit%:33': 0.91; 'genius': 0.91; 'hopes': 0.91; 'mistake': 0.91; 'scientists,': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: __init__ is the initialiser
Date Sat, 01 Feb 2014 10:05:07 +1100
References <lcgtpf$tui$1@ger.gmane.org> <lcgut2$b8r$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host vmx15867.hosting24.com.au
X-Public-Key-ID 0xBD41714B
X-Public-Key-Fingerprint 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:L6sF8PGYXO2/KQzDKbpozWtsnuQ=
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.6228.1391209524.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391209524 news.xs4all.nl 2946 [2001:888:2000:d::a6]:42197
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65148

Show key headers only | View raw


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.

-- 
 \     “This world in arms is not spending money alone. It is spending |
  `\      the sweat of its laborers, the genius of its scientists, the |
_o__)           hopes of its children.” —Dwight Eisenhower, 1953-04-16 |
Ben Finney

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


Thread

Re: __init__ is the initialiser Ben Finney <ben+python@benfinney.id.au> - 2014-02-01 10:05 +1100

csiph-web