Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: using __getitem()__ correctly Date: Thu, 31 Dec 2015 11:21:59 +1100 Lines: 44 Message-ID: References: <56846ddf$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de WJ2qIeEPAU5R5Sq4R5VPVw3NL/GPitR0Dbe7ZTQmRmOQ== Cancel-Lock: sha1:4qPRPsvD7nwM/Qad2uFkZGUBftI= 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; 'attributes': 0.07; 'important,': 0.07; 'incidental': 0.09; 'incorrect': 0.09; 'object;': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:()': 0.09; 'subject:using': 0.09; 'python': 0.10; 'itself.': 0.11; 'syntax': 0.13; 'thu,': 0.15; '10:13': 0.16; 'attributes,': 0.16; 'boolean': 0.16; 'container.': 0.16; 'crashes': 0.16; 'distinct': 0.16; 'distinction': 0.16; 'doesn\xe2\x80\x99t': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'attribute': 0.18; 'integer': 0.18; '2015': 0.20; 'saying': 0.22; 'referring': 0.22; 'am,': 0.23; 'dec': 0.23; 'implemented': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'correct': 0.28; 'crash': 0.29; 'dictionary': 0.29; "i'm": 0.30; 'code': 0.30; 'generally': 0.32; 'class': 0.33; "d'aprano": 0.33; 'items.': 0.33; 'steven': 0.33; 'languages': 0.34; 'could': 0.35; 'important.': 0.35; 'instance': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'difference': 0.38; 'no,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'relationship': 0.61; 'real': 0.62; 'skip:n 10': 0.62; 'to,': 0.63; 'between': 0.65; 'talking': 0.67; 'number:': 0.69; '8bit%:21': 0.70; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; '_o__)': 0.84; 'horrible': 0.84; 'received:125': 0.84; 'syntax;': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101029 Steven D'Aprano writes: > On Thu, 31 Dec 2015 10:13 am, Ben Finney wrote: > > > You may be familiar with other languages where the distinction > > between “attribute of an object” is not distinct from “item in a > > dictionary”. Python is not one of those languages; the distinction > > is real and important. > > I'm not sure what distinction you're referring to, can you explain? Tersely: the relationship between an object and its attributes, is not the same as the relationship between a dictionary and its items. > Obviously there is a syntax difference between x.attr and x['key'] Not merely syntax; the attributes of an object are not generally available as items of the container. > but attributes *are* items in a dictionary That's like saying everything in Python is a number: it conflates the implementation with the semantics. The distinction between a Python integer and a Python boolean value is real and important, despite the incidental fact of their both being implemented as numbers. > Either the instance __dict__, the class __dict__, or a superclass > __dict__. No, I'm not referring to the ‘__dict__’ attribute of an object; I'm referring to the object itself. To talk about the attributes of an object ‘foo’ is distinct from talking about the items in a dictionary ‘foo’. That distinction is real, and important. -- \ “… correct code is great, code that crashes could use | `\ improvement, but incorrect code that doesn’t crash is a | _o__) horrible nightmare.” —Chris Smith, 2008-08-22 | Ben Finney