Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81766
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| 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; 'else:': 0.03; 'iterate': 0.09; 'key.': 0.09; 'override': 0.09; 'undefined': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; "'xxx'": 0.16; '-tkc': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'dict': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterators': 0.16; 'key)': 0.16; 'key):': 0.16; 'key?': 0.16; 'lambda': 0.16; 'received:10.216': 0.16; 'subclass': 0.16; 'subject:key': 0.16; 'tried:': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'keys': 0.31; 'larry': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'but': 0.35; 'there': 0.35; 'i.e.': 0.36; 'subject:skip:d 10': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'received:10': 0.37; 'how': 0.40; 'skip:t 30': 0.61; 'to:addr:gmail.com': 0.65; 'behaviors': 0.74; 'dict.': 0.84; "they'd": 0.84; 'received:108': 0.93; 'subject:Setting': 0.96 |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-MC-Relay | Neutral |
| X-MailChannels-SenderId | wwwh|x-authuser|tim@thechases.com |
| X-MailChannels-Auth-Id | wwwh |
| X-MC-Loop-Signature | 1417458203477:72803271 |
| X-MC-Ingress-Time | 1417458203477 |
| Date | Mon, 1 Dec 2014 12:25:53 -0600 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | Larry Martell <larry.martell@gmail.com> |
| Subject | Re: Setting default_factory of defaultdict to key |
| In-Reply-To | <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> |
| References | <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-AuthUser | tim@thechases.com |
| Cc | "python-list@python.org" <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16462.1417458574.18130.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417458574 news.xs4all.nl 2943 [2001:888:2000:d::a6]:52589 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81766 |
Show key headers only | View raw
On 2014-12-01 13:05, 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?
>
> i.e. if d['xxx'] were accessed and there was no key 'xxx' then
> d['xxx'] would get set to 'xxx'
>
> I know I can define a function with lambda for the default_factory,
> but I don't see how to access the key. I tried:
>
> d = defaultdict(lambda: key)
You could subclass it:
class MyDefaultDict(defaultdict):
def __missing__(self, key):
#self[key] = key # if you actually want it in the dict
return key
You might also have to override the __contains__ method to always
return True if you want
value_not_in_dict = 42
my_default_dict = MyDefaultDict(int)
if value_not_in_dict in my_default_dict:
this_branch_would_always_happen()
else:
this_branch_should_never_happen
You'd also have weird behaviors with iterators as they'd only ever
iterate over things that were in the dict.
-tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Setting default_factory of defaultdict to key Tim Chase <python.list@tim.thechases.com> - 2014-12-01 12:25 -0600
csiph-web