Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'attribute': 0.07; 'explicit': 0.07; 'though:': 0.07; '[1,': 0.09; 'lst': 0.09; 'subject:design': 0.09; 'python': 0.11; 'def': 0.12; "'a',": 0.16; "'b',": 0.16; 'add(self,': 0.16; 'already,': 0.16; 'e):': 0.16; 'fail,': 0.16; 'from:addr:python': 0.16; 'magic': 0.16; 'subject:Language': 0.16; '{2:': 0.16; '{3:': 0.16; '{})': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'value.': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip:l 30': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '(which': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'yields': 0.31; 'class': 0.32; 'stuff': 0.32; '(most': 0.33; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'library.': 0.36; 'method': 0.36; 'thanks': 0.36; 'possible': 0.36; 'behind': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'chain': 0.60; 'mentioned': 0.61; 'skip:n 10': 0.64; 'here': 0.66; 'default': 0.69; 'legal': 0.71; 'encounters': 0.84; 'mistakes,': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools.arcor-ip.net': 0.84; 'cast': 0.91; 'thing,': 0.91; 'many,': 0.93 Date: Wed, 11 Sep 2013 22:41:50 +0200 From: Markus Rother User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130422 Thunderbird/17.0.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Language design References: <522eb795$0$29999$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <522eb795$0$29999$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-bounce-key: webpack.hosteurope.de; python@markusrother.de; 1378933022; eec2f454; 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: 105 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378933023 news.xs4all.nl 15939 [2001:888:2000:d::a6]:46146 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53995 Hello all, Thanks for this thread. Here are my two cents... On 10.09.2013 08:09, Steven D'Aprano wrote: > What design mistakes, traps or gotchas do you think Python has? Gotchas > are not necessarily a bad thing, there may be good reasons for it, but > they're surprising. """ 1. Occasionally, one encounters a strange idiom. Syntactically legal and consistent, though: >>> +4++4 8 >>> -4+-4 -8 >>> -4-+4 -8 2. Reduce removed from standard library. That is a big fail, in my opinion. 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' Instead, this works: >>> class Coll(list): ... ... def add(self, e): ... self.append(e) ... return self ... >>> lst = Coll() >>> lst.add(1).add(2).add(3) ## OK [1, 2, 3] A very practical use case is, that the return value of None makes all of these methods unusable for reduce. >>> from functools import reduce ... >>> many = [{1: 'a'}, {2: 'b'}, {3: 'c'}] ... >>> reduce(lambda d, other : d.update(other), many, {}) ## FAIL Traceback (most recent call last): ... AttributeError: 'NoneType' object has no attribute 'update' Again, one would have to define an update function with an explicit return value. >>> many = [{1: 'a'}, {2: 'b'}, {3: 'c'}] ... >>> def fn(d, other): ... d.update(other) ... return d ... >>> reduce(fn, many, {}) ## OK {1: 'a', 2: 'b', 3: 'c'} 4. As has been mentioned already, some built-in functions do magic stuff behind the scenes: >>> () == [] False But: >>> bool(().__eq__([])) True Because: >>> ().__eq__([]) NotImplemented which yields True when cast to boolean. """ Greets, Markus