Do you have real-world use cases for map's None fill-in feature?

Raymond Hettinger python at rcn.com
Tue Jan 10 12:51:15 EST 2006


[Raymond]
> >ISTM, these cures are worse than the disease ;-)

[Bengt]
> Are you reacting to my turgidly rambling post, or to
>
>  >>> from ut.izip2 import izip2 as izip
>  >>> it = izip('abc','12','ABCD')
>  >>> for t in it: print t
>  ...
>  ('a', '1', 'A')
>  ('b', '2', 'B')
>
> Then after a backwards-compatible izip, if the iterator has
> been bound, it can be used to continue, with sentinel sustitution:
>
>  >>> for t in it.rest('<sentinel>'): print t
>  ...
>  ('c', '<sentinel>', 'C')
>  ('<sentinel>', '<sentinel>', 'D')
>
> or optionally in sentinel substitution mode from the start:
>
>  >>> for t in izip('abc','12','ABCD').rest('<sentinel>'): print t
>  ...
>  ('a', '1', 'A')
>  ('b', '2', 'B')
>  ('c', '<sentinel>', 'C')
>  ('<sentinel>', '<sentinel>', 'D')
>
> Usage-wise, this seems not too diseased to me, so I guess I want to make sure
> this is what you were reacting to ;-)

There is an elegance to the approach; however, if some sort of fill-in
were needed, I'm more inclined to add a separate function than to
append a method to the izip object.  The latter API presents a bit of a
learning/memory challenge, not because it is hard, but because it is
atypical.

A unique advantage for your API is that the loop can be run in two
phases, matched and unmatched. The question then turns to whether there
is a need for that option.  So far, the three threads on the subject
have shown us to be starved for use cases for a single phase
izip_longest function, much less a two-pass version of the same.



Raymond




More information about the Python-list mailing list