Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5419

Re: dict: retrieve the original key by key

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 <python-python-list@m.gmane.org>
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 <cwg@falma.de>
Subject Re: dict: retrieve the original key by key
Date Sun, 15 May 2011 12:46:15 +0200
References <874o4wwenu.fsf@falma.de> <BANLkTikszH_r-PAuL0pJ8reMc+bGafuRew@mail.gmail.com> <mailman.1584.1305450715.9059.python-list@python.org> <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1589.1305456390.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 11:11 +0200
  Re: dict: retrieve the original key by key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-15 10:18 +0000
    Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 12:46 +0200
    Re: dict: retrieve the original key by key Terry Reedy <tjreedy@udel.edu> - 2011-05-15 16:53 -0400
    Re: dict: retrieve the original key by key Chris Rebert <clp2@rebertia.com> - 2011-05-15 14:19 -0700
    Re: dict: retrieve the original key by key Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-15 18:47 -0600

csiph-web