Pre-PEP: Dictionary accumulator methods

Michael Spencer mahs at telcopartners.com
Mon Mar 28 13:17:38 EST 2005


Jack Diederich wrote:
> On Sun, Mar 27, 2005 at 02:20:33PM -0700, Steven Bethard wrote:
> 
>>Michele Simionato wrote:
>>
>>>I am surprised nobody suggested we put those two methods into a
>>>separate module (say dictutils or even UserDict) as functions:
>>>
>>
>>>from dictutils import tally, listappend
>>
>>>tally(mydict, key)
>>>listappend(mydict, key, value)
>>
>>Sorry to join the discussion so late (I've been away from my email for a 
>>week) but this was exactly my reaction too.  In fact, I have a 
>>'dicttools' module with similar methods in it:
> 
> <snipped>
> 
> I like this approach, it will give us a chance to test & tweak the signature
> before hanging it off dict proper.  It feels similar to the strings module 
> to str transition, sets module to set builtin, and itertools module to iter
> transition.
> 
> itertools to iter transition, huh?  I slipped that one in, I mentioned it
> to Raymond at PyCon and he didn't flinch.  It would be nice not to have to
> sprinkle 'import itertools as it' in code.  

Not that that is such a pain, but accessing itertools functions from an 
"outlying" module seems somewhat incompatible with putting iterative approaches 
center stage.

iter could also become a type
> wrapper instead of a function, so an iter instance could be a wrapper that
> figures out whether to call .next or __getitem__ depending on it's argument.
> for item in iter(mylist).imap:
>   print item

Also opening the door for iter to be subclassed.  For example, could listiter 
become a specialization of iter -  one that uses __getitem__ and which could 
allow reverse iteration?

> or
> for item in iter.imap(mylist):
>   print item
> 
> I haven't digested that too much, just a thought.
> 
> -jackdied

A very good thought IMO.

Michael




More information about the Python-list mailing list