Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Terry Reedy
On 12/5/2014 3:51 PM, John J Posner wrote: The defaultdict documentation is confusing on this point. A *long* time ago, I filed Bug 9536 to improve the doc, but the fix hasn't bubbled to the surface yet. Untrue. Your patch 'bubbled to the surface' and got provisionally rejected in 5 hours an

Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Ian Kelly
On Dec 7, 2014 9:33 AM, "Dave Angel" wrote: > > On 12/05/2014 03:51 PM, John J Posner wrote: >> >> At the beginning of this thread, Ian Kelly said: > > > Since this clearly is intended to be part of the earlier thread, please make it so by using reply-list or whatever equivalent your email program

Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Dave Angel
On 12/07/2014 11:43 AM, Shiyao Ma wrote: On Dec 07 at 11:31 -0500, Dave Angel wrote: Since this clearly is intended to be part of the earlier thread, please make it so by using reply-list or whatever equivalent your email program has. Kinda OT. But interested what's the difference between repl

Different “reply” functions: reply-to-sender, reply-to-list, reply-to-all (was: Setting default_factory of defaultdict to key)

2014-12-07 Thread Ben Finney
Shiyao Ma writes: > On Dec 07 at 11:31 -0500, Dave Angel wrote: > > Since this clearly is intended to be part of the earlier thread, > > please make it so by using reply-list or whatever equivalent your > > email program has. > > Kinda OT. But interested what's the difference between reply-list a

Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Shiyao Ma
On Dec 07 at 11:31 -0500, Dave Angel wrote: > Since this clearly is intended to be part of the earlier thread, please make > it so by using reply-list or whatever equivalent your email program has. Kinda OT. But interested what's the difference between reply-list and to. In addition, based on what

Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Dave Angel
On 12/05/2014 03:51 PM, John J Posner wrote: At the beginning of this thread, Ian Kelly said: Since this clearly is intended to be part of the earlier thread, please make it so by using reply-list or whatever equivalent your email program has. Not with defaultdict, but you can subcla

Re: Setting default_factory of defaultdict to key

2014-12-07 Thread John J Posner
At the beginning of this thread, Ian Kelly said: Not with defaultdict, but you can subclass dict and provide a __missing__ method directly To emphasize, you don't need to subclass "defaultdict" -- you need only subclass "dict" itself: class MyDict(dict): def __missing__(self, key):

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Ian Kelly
On Mon, Dec 1, 2014 at 11:29 AM, Larry Martell wrote: > I spoke too soon: > class defaultdictkey(defaultdict): > ... def __missing__(self, key): > ... self[key] = self.default_factory(key) > ... x = defaultdictkey(lambda k: k) print x['aaa'] > None print x['aaa'] > aaa

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Larry Martell
On Mon, Dec 1, 2014 at 1:36 PM, Ethan Furman wrote: > On 12/01/2014 10:29 AM, Larry Martell wrote: >> On Mon, Dec 1, 2014 at 1:19 PM, Ethan Furman wrote: >>> 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 un

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Ethan Furman
On 12/01/2014 10:29 AM, Larry Martell wrote: > On Mon, Dec 1, 2014 at 1:19 PM, Ethan Furman wrote: >> 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 subcla

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Tim Chase
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

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Larry Martell
On Mon, Dec 1, 2014 at 1:19 PM, Ethan Furman wrote: > 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

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Larry Martell
On Mon, Dec 1, 2014 at 1:19 PM, Ethan Furman wrote: > 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

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Ethan Furman
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 key: --> class defaultdictkey(defaultdict): ... def __miss

Re: Setting default_factory of defaultdict to key

2014-12-01 Thread Ian Kelly
On Mon, Dec 1, 2014 at 11: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? > > 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

Setting default_factory of defaultdict to key

2014-12-01 Thread Larry Martell
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