any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
> > Py2.5 is already going to include any() and all() as builtins.  The
> > signature  does not include a function, identity or otherwise.
> > Instead, the caller can
> > write a listcomp or genexp that evaluates to True or False:
> >
> > any(x >= 42 for x in data)

[Roose]
> Oh great, I just saw that.
 . . .
> But I wish it could be included in Python 2.4.x.

If it is any consolation, the any() can already be expressed somewhat cleanly
and efficiently in Py2.4 with genexps:

  True in (x >= 42 for x in data)

The translation for all() is a little less elegant:

  False not in (x >= 42 for x in data)


Raymond Hettinger


-- 
http://mail.python.org/mailman/listinfo/python-list


any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Roose]
> Actually I was just looking at Python 2.5 docs since you mentioned this.
>
> http://www.python.org/dev/doc/devel/whatsnew/node3.html
>
> It says min() and max() will gain a key function parameter, and sort()
> gained one in Python 2.4 (news to me).

It also appears in itertools.groupby() and, for Py2.5, in heapq.nsmallest() and
heapq.nlargest().


> And they do indeed default to the identity in all 3 cases, so this seems
> very inconsistent.  If one of them has it, and sort gained the argument even
> in Python 2.4 with generator expressions, then they all should have it.
>
> > any(x >= 42 for x in data)
>
> Not to belabor the point, but in the example on that page, max(L, key=len)
> could be written max(len(x) for x in L).

Think about it.  A key= function is quite a different thing.  It provides a
*temporary* comparison key while retaining the original value.  IOW, your
re-write is incorrect:

>>> L = ['the', 'quick', 'brownish', 'toad']
>>> max(L, key=len)
'brownish'
>>> max(len(x) for x in L)
8


Remain calm.  Keep the faith.  Guido's design works fine.

No important use cases were left unserved by any() and all().



Raymond Hettinger


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Roose
Ah OK, I stand corrected.  Whoops.  I just read the web page and thought the
wrong thing, that makes sense.

> Think about it.  A key= function is quite a different thing.  It provides
a
> *temporary* comparison key while retaining the original value.  IOW, your
> re-write is incorrect:
>
> >>> L = ['the', 'quick', 'brownish', 'toad']
> >>> max(L, key=len)
> 'brownish'
> >>> max(len(x) for x in L)
> 8
>
>
> Remain calm.  Keep the faith.  Guido's design works fine.
>
> No important use cases were left unserved by any() and all().
>
>
>
> Raymond Hettinger
>
>


-- 
http://mail.python.org/mailman/listinfo/python-list