Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Pythonic style Date: Thu, 28 Apr 2016 13:56:05 +1000 Lines: 20 Message-ID: References: <5720357B.4060009@icloud.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> <572188F8.7010304@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de yRxL02UWI9PHV+SfhUFJOQZdKGBnyjuVBFf4sMgIS4Ag== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'received:209.85.223': 0.03; 'cc:addr:python-list': 0.09; 'methods,': 0.09; 'thu,': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trying': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '~ethan~': 0.29; 'classes': 0.30; 'skip:s 30': 0.31; 'maybe': 0.33; 'point': 0.33; 'rule': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'your': 0.60; 'special': 0.73; 'chrisa': 0.84; 'to:none': 0.91; 'ethan': 0.91; 'furman': 0.91 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:cc; bh=83kwRynn+cN12KUQUwREIyf/15SDHVn/oFXpU6HE4r4=; b=PagJTORtGZR9EIw3KTJ7LrkBrKUwgdxJnAVt5UVyySRRO+1NORFu7s824q5X92VzXm 9RsWZhIUmW8X6quk7wWDyEgUK8NF6XwVJ6ym1koydHZQw7DWnLKpvQJckt5WoXZEcPmN 2hdMswQJDqvkJy8272700y3n4uNeA0dF4hIc8etM8eIyMzPFhUDGFKGunqscYesz+8ZI /MWqc0KQ8YvXXcTHjLUIkako6Y3Zi1MXbM8oVXslouPKeq4nBZEFeEYzmduxsE1eO3i4 hJi8UBBT3qU+32+GAuR+HC5lM7+e7oYHqzxZ5l47i/ISsDQfQFevQZA0N+DaPbdeXz1x NEng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=83kwRynn+cN12KUQUwREIyf/15SDHVn/oFXpU6HE4r4=; b=HguOO9NJx0QmoQIa/RwlwGyEeJX8mvKMG7GC/NrpTlmLVBwA7aACsq8tDPIoBTMyIv uLsx+BIo8eWGRzuJNTIGB0KYhI2uY7/D5IQVlbjdPmaNyVEVVlUhnLGs59m9kzbDzcNN bHy1eyzjg5Z5z2T34gAAoy9sYaVub2f5TyS5QKCIuji/BTPjZBFNz9fm2wtnbkzX3u3n EVoJuz3d2SkYbtJgGeJvEE+iinX/Ac62/QVmeZdXViVtfJTk2KxRo2Ki653s5PN5f7SJ dnrHzaKaullwHqeDK/noR/4NtSpQiBja7/eFOFQcdmo1f41aZDi0++TMLe9Ph3H6cjnx 7ABA== X-Gm-Message-State: AOPr4FU6mo361cvv2cpybvzTVbDqKMe5jATE4UNvmyS/lKhlo2Qojpluw65jYN88d6naXZgD9reN1ZU/iBdtpA== X-Received: by 10.107.137.166 with SMTP id t38mr14888105ioi.31.1461815765251; Wed, 27 Apr 2016 20:56:05 -0700 (PDT) In-Reply-To: <572188F8.7010304@stoneleaf.us> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <5720357B.4060009@icloud.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> <572188F8.7010304@stoneleaf.us> Xref: csiph.com comp.lang.python:107750 On Thu, Apr 28, 2016 at 1:52 PM, Ethan Furman wrote: > > The point Ben was trying to make is this: you should never* call __dunder__ > methods in normal code; there is no need to do so: > > - use len(), not __len__() > - use next(), not __next__() > - use some_instance.an_attribute, not some_instance.__dict__['an_attribute'] > > -- > ~Ethan~ > > * Okay, maybe /almost/ never. About the only time you need to is when > giving your classes special methods, such as __add__ or __repr__. My rule of thumb is: Dunders are for defining, not for calling. It's not a hard-and-fast rule, but it'll get you through 99%+ of situations. ChrisA