Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(of': 0.07; 'intermediate': 0.07; 'method.': 0.07; 'variables': 0.07; '__init__': 0.09; 'attributes': 0.09; 'constructor': 0.09; 'deemed': 0.09; 'method,': 0.09; 'methods,': 0.09; 'run,': 0.09; 'python': 0.11; 'jan': 0.12; 'instance:': 0.16; 'overriding': 0.16; 'subclass': 0.16; 'subclasses': 0.16; 'wished': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; '(in': 0.22; 'java': 0.24; "i've": 0.25; 'define': 0.26; 'nearly': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'generally': 0.29; 'tim': 0.29; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'steven': 0.31; 'allows': 0.31; 'class': 0.32; 'probably': 0.32; 'languages': 0.32; 'run': 0.32; 'fri,': 0.33; 'to:name:python- list': 0.33; 'subject:the': 0.34; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'method': 0.36; 'example,': 0.37; 'two': 0.37; 'being': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'expect': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'ensure': 0.60; 'eventually': 0.60; 'most': 0.60; 'complete': 0.62; 'invalid': 0.68; 'default': 0.69; 'construction': 0.72; 'batchelder': 0.84; 'mistake': 0.91; 'state.': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+4iYU/YKyvD+gwuxdjZJKW3s/utTluYrz568S+wmU/k=; b=kQ+DA00WvMDGghCJB7CnKpAZGwa9oz0eJj7uFeWDb9DvDtBAweGUs9KVKSl52TmP8E 9qvtYHhAo0KLgGihEEBgKKALFkh321CMtmOxCX+ReTg741qWnEG8Ft7+4CaG5HDudO6u 3ffMGH/pL7t7auvUkTyywz0Ajy1bi5HfFb2XwrdOZjIfksZGgIXqF+YJw4nfGeZkBSYN fSqwqX+WKQnzG8DwAXApqRAtuUec2DPANM1UjvFbwtAU6BlSEGylLIyCRYgQ9Dc04013 irL34ZdxhWC18SSnnuHE+bmzl4WwERA75L4hr8ZEH90Iw4cEgeESZAMfAaG9TjQzr4H9 6F+A== MIME-Version: 1.0 X-Received: by 10.182.194.78 with SMTP id hu14mr27446907obc.8.1391385738012; Sun, 02 Feb 2014 16:02:18 -0800 (PST) In-Reply-To: <52ec6d1f$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <52ec6d1f$0$29972$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 3 Feb 2014 11:02:17 +1100 Subject: Re: __init__ is the initialiser From: Tim Delaney To: Python-List Content-Type: multipart/alternative; boundary=f46d044519b9a8ee4404f1753c9f X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391385741 news.xs4all.nl 2938 [2001:888:2000:d::a6]:45499 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65299 --f46d044519b9a8ee4404f1753c9f Content-Type: text/plain; charset=UTF-8 On 1 February 2014 14:42, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > On Fri, 31 Jan 2014 14:52:15 -0500, Ned Batchelder wrote: > > (In hindsight, it was probably a mistake for Python to define two create- > an-object methods, although I expect it was deemed necessary for > historical reasons. Most other languages make do with a single method, > Objective-C being an exception with "alloc" and "init" methods.) > I disagree. In nearly every language I've used which only has single-phase construction, I've wished for two-phase construction. By the time you get to __init__ you know the following things about the instance: 1. It is a complete instance of the subclass - there's no part of the structure that is invalid to access (of course, many attributes might not yet exist). 2. Calling a method from __init__ will call the subclass' method. This allows subclasses to hook into the initisation process by overriding methods (of course, the subclass will need to ensure it has initialised all the state it needs). This is generally not allowed in languages with single-phase construction because the object is in an intermediate state. For example, in C++ the vtable is for the class currently being constructed, not the subclass, so it will always call the current class' implementation of the method. In Java you can actually call the subclass' implementation, but in that case it will call the subclass method before the subclass constructor is actually run, meaning that instance variables will have their default values (null for objects). When the base class constructor is eventually run the instance variables will be assigned the values in the class definition (replacing anything set by the subclass method call). Tim Delaney --f46d044519b9a8ee4404f1753c9f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 1= February 2014 14:42, Steven D'Aprano <steve+comp.l= ang.python@pearwood.info> wrote:
On Fri, 31 Jan 2014 14:52:= 15 -0500, Ned Batchelder wrote:

(In hindsight, it was probably a mistake for Python to define two create- an-object methods, although I expect it was deemed necessary for
historical reasons. Most other languages make do with a single method,
Objective-C being an exception with "alloc" and "init" = methods.)

I disagree. In nearly every l= anguage I've used which only has single-phase construction, I've wi= shed for two-phase construction. By the time you get to __init__ you know t= he following things about the instance:

1. It is a complete instance of the subclass - there= 9;s no part of the structure that is invalid to access (of course, many att= ributes might not yet exist).

2. Calling a method = from __init__ will call the subclass' method. This allows subclasses to= hook into the initisation process by overriding methods (of course, the su= bclass will need to ensure it has initialised all the state it needs). This= is generally not allowed in languages with single-phase construction becau= se the object is in an intermediate state.

For example, =C2=A0in C++ the vtable is for the class c= urrently being constructed, not the subclass, so it will always call the cu= rrent class' implementation of the method.

In = Java you can actually call the subclass' implementation, but in that ca= se it will call the subclass method before the subclass constructor is actu= ally run, meaning that instance variables will have their default values (n= ull for objects). When the base class constructor is eventually run the ins= tance variables will be assigned the values in the class definition (replac= ing anything set by the subclass method call).

Tim Delaney=C2=A0
--f46d044519b9a8ee4404f1753c9f--