Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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; 'value,': 0.04; 'argument': 0.05; 'attribute': 0.07; '[1,': 0.09; 'lst': 0.09; 'method,': 0.09; 'subject:design': 0.09; "wouldn't": 0.14; 'agree.': 0.16; 'argument,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mutated': 0.16; 'programmer,': 0.16; 'sort()': 0.16; 'sorted()': 0.16; 'subject:Language': 0.16; 'url:sort': 0.16; 'wrote:': 0.18; "python's": 0.19; 'split': 0.19; 'thu,': 0.19; '>>>': 0.22; '(in': 0.22; 'convenient': 0.24; 'skip:l 30': 0.24; '(or': 0.24; 'this:': 0.26; 'primary': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'crash': 0.31; 'sep': 0.31; 'url:se': 0.31; 'worked': 0.33; '(most': 0.33; 'entirely': 0.33; 'created': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'possible': 0.36; 'should': 0.36; 'behind': 0.37; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'called': 0.40; 'chain': 0.60; 'new': 0.61; 'times': 0.62; 'default': 0.69; 'potentially': 0.81; "it'd": 0.84; 'reasoning': 0.91; '2013': 0.98 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=DSgqMwLnv2N5wGuHXqW9LJ1O6rs9GDx67KNcTjTyqBY=; b=YRZvwFGf4XSmvd93OcAvF5RH7tsyN9THi2e67UI8GmQ6V20YtCZwdozbuoliM6bFoR Nn6nmKoKRe8FqQJYO4tJSIn4zT2loiV+j92W+0Aat04rpgPvV61VaCY4y/yYyF1fN9U7 RnuOUqW7XzEJvia0IaS31cKlVL6LzWM8UyMJpNH1BRjCVmp7i//1+8ZvbcMDkv97ti0S ixs4iA/5BtNc556cnS/wIlYfqLNfwcWDABdpjrR8PxsOg/AFaOT7aeQZ6VzybM2Jtkpi 8ZZRoSq2FLYeJ0XSVdR2XnOfEMdbv31PAqTUz0ervGikhTNd8ghLyp6w3PotQw5Qf9Fu lDtA== MIME-Version: 1.0 X-Received: by 10.52.98.66 with SMTP id eg2mr3059559vdb.24.1378942038826; Wed, 11 Sep 2013 16:27:18 -0700 (PDT) In-Reply-To: <5230D58E.9020508@markusrother.de> References: <522eb795$0$29999$c3e8da3$5496439d@news.astraweb.com> <5230D58E.9020508@markusrother.de> Date: Thu, 12 Sep 2013 09:27:18 +1000 Subject: Re: Language design From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378942041 news.xs4all.nl 15882 [2001:888:2000:d::a6]:39042 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54006 On Thu, Sep 12, 2013 at 6:41 AM, Markus Rother wrote: > 3. The default return value of methods is None instead of self. > If it was self, it would be possible to chain method calls (which > is called a cascade in smalltalk). > > > >>> lst = [] > >>> lst.append(1).append(2).append(3) ## FAIL > Traceback (most recent call last): > ... > AttributeError: 'NoneType' object has no attribute 'append' That's a policy decision: a method (or function) will *EITHER* return a value, *OR* mutate its primary argument (in the case of a method, that's self). It reduces the chances of code like this: foo = [1, 4, 2, 8, 5, 7] largest_digit = foo.sort()[-1] one_seventh = ''.join(map(str,foo)) If you used sorted(foo) instead of foo.sort(), this wouldn't crash out, and it'd do what you expect. Having foo.sort() return self would mean this wouldn't crash, but would potentially do something surprising. But while I understand the reasoning behind it, I don't entirely agree. There are times when I use Pike's sort() function [1], which does return its mutated argument, something like this: foo = sort(blah_blah_blah()) Why should that be split into two statements? Or alternatively, why should an extra copy of the list be created (if you use Python's sorted() here)? But for the new programmer, this is a convenient safety-net, and if list.sort() worked the other way, it'd be just as much a gotcha ("I ask for a sorted list, and it also changed the original?!??"). ChrisA [1] http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/sort.html