Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!news2.euro.net!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; 'cache': 0.04; 'memory.': 0.05; 'cached': 0.07; 'dictionary': 0.07; 'though.': 0.07; 'python': 0.07; '+0200,': 0.09; 'graph': 0.09; 'object.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'sun,': 0.09; 'def': 0.13; 'wrote:': 0.14; 'dictionary,': 0.16; 'equal,': 0.16; 'stumbled': 0.16; 'subject:key': 0.16; 'far.': 0.19; 'writes:': 0.20; 'keys': 0.23; 'objects': 0.24; 'function': 0.27; 'thanks': 0.29; 'strings,': 0.31; 'equal': 0.31; 'to:addr:python- list': 0.32; 'another': 0.32; 'idea': 0.32; 'header:X-Complaints- To:1': 0.34; 'actually': 0.34; 'requires': 0.35; 'header:User- Agent:1': 0.35; 'charset:us-ascii': 0.36; 'steven': 0.38; 'but': 0.38; 'used': 0.38; 'received:org': 0.38; 'received:82': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'would': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'hey,': 0.74; 'subject:original': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christoph Groth Subject: Re: dict: retrieve the original key by key Date: Sun, 15 May 2011 12:46:15 +0200 References: <874o4wwenu.fsf@falma.de> <4dcfa885$0$29996$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: dra38-5-82-246-248-175.fbx.proxad.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:E4Wwl0HEoxhjHG8UaPymxXPRgNo= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 28 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305456390 news.xs4all.nl 41113 [::ffff:82.94.164.166]:45139 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5419 Steven D'Aprano writes: > On Sun, 15 May 2011 11:11:41 +0200, Christoph Groth wrote: > >> I would like to avoid having _multiple_ objects which are equal (a == >> b) but not the same (a is not b). This would save a lot of memory. > > Based on the idea of interning, which is used for Python strings: > > cache = {} def my_intern(obj): > return cache.setdefault(obj, obj) > > > x = make_some_object() x = my_intern(x) > > This ensures that equal objects in the graph are not just equal, but > the same cached object. This requires another dictionary, though. But hey, they keys of my dictionary are actually strings, so I can use the built-in intern. Somehow, I have never stumbled accross this built-in function so far. Thanks a lot for the hint! Christoph