Fast constant functions for Py2.5's defaultdict()

Michele Simionato michele.simionato at gmail.com
Wed Feb 14 03:44:42 EST 2007


On Feb 14, 9:11 am, "Raymond Hettinger" <pyt... at rcn.com> wrote:
> On Feb 13, 5:09 pm, Giovanni Bajo <n... at ask.me> wrote:
>
> > > The itertools.repeat(const).next approach wins on speed and
> > > flexibility.
>
> > But it's the most unreadable too.
>
> Not really.  It's unusual but plenty readable (no surprise that
> repeat(0) repeatedly gives you zero).  I think it more surprising that
> int() with no arguments gives you a zero.

Well, if I was doing code review of some of my coworkers I would ask
them
to use them int if the constant was zero and lambda otherwise. If they
wanted
to use itertools.repeat(const).next they should prove me that the
speed
increase is absolutely significant in their actual use case and
they should put a big comment in the code explaining why they
preferred
the cryptic defaultdict(itertools.repeat(0).next) over the obvious
defaultdict(int).

 Michele Simionato




More information about the Python-list mailing list