Suggesting a new feature - "Inverse Generators"

Michael Spencer mahs at
Fri Mar 25 21:07:23 CET 2005

Scott David Daniels wrote:
> Michael Spencer wrote:
>> itertools.groupby enables you to do this, you just need to define a 
>> suitable grouping function, that stores its state:
> Michael, this would make a great Python Cookbook Recipe.
OK, will do.  What would you call it?  Something like: "Stateful grouping of 
iterable items"

> Nice, but I think "record" is a bit opaque semantically.
> How about group_id or generate_incrementing_unique_id_for_each_group_to_group_by or such?
> Regards,
> Bengt Richter

Agreed, it's an issue.  I think the most natural name is groupby - but that 
would cause more trouble.  What do you think about 'grouping' ?
I would use 'generate_incrementing_unique_id_for_each_group_to_group_by', but 
then people might think I'm trying to outdo Bob Ippolito :-)

> I think your example would
> be more clear for Jordan if you used function attributes:
> def record(item):
>      if len(item) > 20:
>          record.seq +=1
>      return record.seq
> record.seq = 0

That does read better than the mutable default argument hack.  Is this use of 
function attributes generally encouraged? (I tend to think of func_dict for 
meta-data, used only outside the function)  Thoughts?


More information about the Python-list mailing list