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

2007-02-14 Thread Michele Simionato
On Feb 14, 9:11 am, "Raymond Hettinger" <[EMAIL PROTECTED]> wrote: > On Feb 13, 5:09 pm, Giovanni Bajo <[EMAIL PROTECTED]> 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 r

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

2007-02-14 Thread Raymond Hettinger
On Feb 13, 5:09 pm, Giovanni Bajo <[EMAIL PROTECTED]> 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

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

2007-02-13 Thread Giovanni Bajo
On 13/02/2007 20.01, Raymond Hettinger wrote: > FWIW, here are three ways of writing constant functions for > collections.defaultdict(): > > d = defaultdict(int) # slowest way; works only for zero > d = defaultdict(lambda: 0) # faster way; works for any constant > d = defaultdict(it

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

2007-02-13 Thread Raymond Hettinger
FWIW, here are three ways of writing constant functions for collections.defaultdict(): d = defaultdict(int) # slowest way; works only for zero d = defaultdict(lambda: 0) # faster way; works for any constant d = defaultdict(itertools.repeat(0).next)# fastest way; works for any con