Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exception': 0.03; 'attribute': 0.05; 'bug.': 0.07; 'happen,': 0.07; 'python': 0.09; '+return': 0.09; 'dict': 0.09; 'lookup': 0.09; 'name)': 0.09; 'name):': 0.09; 'second.': 0.09; 'subject:using': 0.09; 'bug': 0.10; 'def': 0.10; 'times,': 0.13; 'library': 0.15; 'dec': 0.15; "hasn't": 0.15; 'properly': 0.15; '-return': 0.16; '1.7': 0.16; '2.7:': 0.16; 'accesses': 0.16; 'call?': 0.16; 'camp': 0.16; 'deprecated.': 0.16; 'getattribute': 0.16; 'intuition': 0.16; 'manageable': 0.16; 'measured': 0.16; 'name),': 0.16; 'simpler,': 0.16; 'tighter': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'implementing': 0.17; 'shell': 0.18; '>>>': 0.18; 'code,': 0.18; 'code.': 0.20; 'variable': 0.20; 'advise.': 0.22; 'machine.': 0.22; 'noted': 0.22; 'skip:_ 20': 0.22; 'this:': 0.23; "i've": 0.23; 'seems': 0.23; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; '(e.g.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'noticed': 0.28; '1.3': 0.29; 'faster,': 0.29; 'subsequently': 0.29; 'thinks': 0.29; 'probably': 0.29; "i'm": 0.29; 'received:209.85.215.46': 0.30; 'basic': 0.30; 'code': 0.31; 'december': 0.32; 'aside': 0.32; 'running': 0.32; 'could': 0.32; 'instances': 0.33; 'substantial': 0.33; 'wednesday': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'skip:d 20': 0.34; 'version': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'compared': 0.35; 'replaced': 0.35; 'pm,': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'add': 0.36; 'really': 0.36; 'except': 0.36; 'but': 0.36; '12,': 0.36; 'method': 0.36; 'anything': 0.36; 'should': 0.36; 'too': 0.36; 'enough': 0.36; 'bad': 0.37; 'does': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'easy': 0.60; 'real': 0.61; 'worth': 0.63; 'ever': 0.63; 'more': 0.63; 'making': 0.64; 'here': 0.65; 'choose': 0.65; 'business': 0.70; 'million': 0.72; '40%.': 0.84; 'about,': 0.84; 'common,': 0.84; 'ian,': 0.84; 'jihad': 0.84; 'to:name:python': 0.84; 'duly': 0.91; 'innocent': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=7hF3oiQ5SaKaUE25xkmGpyrhZ5dzD3UBxU92uJ1oEf0=; b=BoXAQpt0/axtwHosOJrNy+BDM9/wjn9iqgx+MC45uHN/v/L3aEBmG15vX4fz8hBAmz MF2fxAbcO85FhBqdclTxQql9ZM3Z1zdhx56SaCwnCPZVCrbr0oaxAMSILYuQWRgNPXRi F8zPzUapIkKk/iBoQ5xQ+A25jiMLDwB8pRLA/aia/T/edyO/YNqT/AxOLi+29VP92ezV k3pjxZ9jL0TZBOHEzwD0nG0v33A/vE7kypqLzvjrqvUtBoTb8ZjyrprI0fblCZtgcN+q 6viCMOg7H8HhD3m1uxB+mwqqg68RwAfD2u+CW1MhBFh5iWJCI0xn4m7gUYL2qnyJYwUe DFrg== MIME-Version: 1.0 In-Reply-To: <201212121720.54027.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <201212121720.54027.dave@cinege.com> From: Ian Kelly Date: Wed, 12 Dec 2012 16:12:58 -0700 Subject: Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes To: Python 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355354012 news.xs4all.nl 6911 [2001:888:2000:d::a6]:59564 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34731 On Wed, Dec 12, 2012 at 3:20 PM, Dave Cinege wrote: > On Wednesday 12 December 2012 15:42:36 Ian Kelly wrote: > >> def __getattribute__(self, name): >> if name.startswith('__') and name.endswith('__'): >> return super(Thesaurus, self).__getattribute__(name) >> return self.__getitem__(name) > > Ian, > > Tested, and works as you advertised. > > Isn't super() depreciated? I've replaced it with this: > -return super(Thesaurus, self).__getattribute__(name) > +return dict.__getattribute__(self, name) It's not deprecated. Some people consider it harmful, and others disagree. I was once in the former camp but have shifted somewhat toward the latter. > Aside from a more palatable result in the python shell for otherwise bad > code...does this get me anything else? Is it really worth the performance hit > of 2 string comparisons for every getattribute call? It could affect real code, not just interactive code. Any time you unthinkingly choose an attribute name that happens to be the name of a dict method (e.g. 'items', which otherwise seems like a rather innocent variable name), that's a potential bug. Depending on how that attribute is subsequently accessed, the bug might not even be noticed immediately. The performance hit compared to the __getattr__ version on my system is about 1.3 microseconds per call, as measured by timeit, or around 40%. For comparison, the performance hit of using the __getattr__ version versus just using a global variable is about 1.7 microseconds per call, or around 4000%. For my own use, I don't consider that substantial enough to worry about, as I'm not in the business of writing code that would be making hundreds of thousands of accesses per second. > Should the idea of implementing what Thesaurus does in mainline python ever > happen, those 10 lines of code will likely spark a 3 month jihad about how to > properly do in python which up until now hasn't been something you do in > python. The basic idea of proxying attribute access on a dict to key lookup is pretty common, actually. It likely won't ever make it into the standard library because 1) there's no clear agreement on what it should look like; 2) it's easy to roll your own; and 3) it looks too much like JavaScript. That last probably isn't valid; attribute proxying is annoying and cumbersome when it automatically happens on every single object in the language; it's much more manageable when you have a single type like Thesaurus that you can use only in the instances where you actually want it. > To me for i in range(len(l)) seems like simpler, faster, tighter code for this > now. It's duly noted that enumerate() is more python and I'm an old fart that > still thinks too much in state machine. I've add except Exception per your > advise. Your intuition about what "seems faster" can lead you astray. Using Python 2.7: >>> timerD = timeit.Timer('for i in range(len(seq)): x = seq[i]', 'seq = range(5)') >>> timerE = timeit.Timer('for i, x in enumerate(seq): pass', 'seq = range(5)') >>> min(timerD.repeat(3)) 0.8711640725291545 >>> min(timerE.repeat(3)) 0.7172601545726138 Of course, that's running each loop a million times, so the difference here really is pretty negligible.