Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'undefined': 0.09; '~ethan~': 0.09; 'def': 0.12; '10:05': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'filename:fname piece:signature': 0.16; 'key):': 0.16; 'key?': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; '{})': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'along': 0.30; 'keys': 0.31; 'larry': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'there': 0.35; 'subject:skip:d 10': 0.36; 'charset:us- ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'subject:Setting': 0.96 Date: Mon, 01 Dec 2014 10:19:24 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Setting default_factory of defaultdict to key References: In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GaXx45WdU89inC4uvdNlRijJTAAKhhLAn" 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417457972 news.xs4all.nl 2867 [2001:888:2000:d::a6]:42235 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81763 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --GaXx45WdU89inC4uvdNlRijJTAAKhhLAn Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable On 12/01/2014 10:05 AM, Larry Martell wrote: > > Is there a way to set the default_factory of defaultdict so that > accesses to undefined keys get to set to the key? You need to subclass and modify __missing__ to actually pass along the ke= y: --> class defaultdictkey(defaultdict): =2E.. def __missing__(self, key): =2E.. self[key] =3D self.default_factory(key) =2E.. return self[key] =2E.. and in action: --> huh =3D defaultdictkey(lambda k: k) --> huh defaultdict( at 0x7fe1305de3f0>, {}) --> huh['x'] 'x' --> huh['x'] 'x' --> huh.get('y') --> huh['y'] 'y' --> huh.get('y') 'y' -- ~Ethan~ --GaXx45WdU89inC4uvdNlRijJTAAKhhLAn Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBAgAGBQJUfLEvAAoJENZ7D1rrH75NqGQP/RaEIfHQz3fgTpp3JcVLEnHG gaIOwU61TfQxyVKO3wk4lMOIhKECcLzCMSy0KAmmYmoMq7NlweZ+eGJxUeZyKTsF KcR0FgUT64fWuAJytD49qpb4s4i6XBJ0X3tG0P+pnRWwxLJ3cs2L982O20u6o6P3 enKQgvo54m/CZMKW8d2Grzf633TtGMQJy6GMtyrzoe3rnTrHseNlFQ0bTRhkmK9r 9drGvd21wu3Z6YUw/ZN5M0JJDg8roNVoHbKTuuVchyNkS9L6ysRglqdgkQ4oDQXp Oo/mX6YgAjxFka1SrmSSIaESQo30yDm7scF9bdbRtVPigI/Q/GDAmjJ5ZpzBqfe8 IAm3NDDoOFUT5ctiAHjAZkh+i4lB8HbJG2Bs1wVkp5To+Xc4X2YbaVzvGGXrR/ko zIKiGd7OtugvSXbVrP6EvJxck9gNLuJOCSF0FNWwZiusGoJf5VeVc8AdLEH9PgTj Wq1n9ZnZ4C4E54cuc8XgUIpxwYHc3QmQTvSRo7s/Rl16mnJBhWSQHYbFrjj9k0x2 db+GVtI9xBmGTVOR5AphAmwTg04YliFF7iBngntzV8Q7yWiQlZOwNT5ifaLg9MSZ 4JsjnHFWVmrmojBYmO+hckHxFsfGGMbqT/6+e7qA+AKJmQfbtl61EBR69KJ6t1xB mjc/NkNLbGe3tcqP+WYp =8XrF -----END PGP SIGNATURE----- --GaXx45WdU89inC4uvdNlRijJTAAKhhLAn--