Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'explicitly': 0.05; 'differently': 0.07; 'initialize': 0.07; 'returned.': 0.07; '__init__': 0.09; 'arguments': 0.09; 'attributes': 0.09; 'code"': 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; 'language.': 0.14; '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; 'language': 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; 'anywhere': 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; 'needed': 0.38; '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; 'our': 0.64; 'more': 0.64; 'situation': 0.65; 'charset:windows-1252': 0.65; 'here': 0.66; 'special': 0.74; '95%': 0.84; 'batchelder': 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: Mark Lawrence Subject: Re: __init__ is the initialiser Date: Fri, 31 Jan 2014 20:17:21 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: host-78-147-20-107.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391199439 news.xs4all.nl 2910 [2001:888:2000:d::a6]:35028 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65138 On 31/01/2014 19:52, Ned Batchelder wrote: > 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? > To clarify the situation I think we should call __new__ "the thing that instanciates an instance of a class" and __init__ "the thing that isn't actually needed as you can add attributes to an instance anywhere in your code" :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence