'for every' and 'for any'

Peter Hansen peter at engcorp.com
Sun May 26 09:33:23 EDT 2002


Oren Tirosh wrote:
> 
> Here's an idea for a possible language enhancement.  I'd like to hear your
> comments about it.  It's inspired by list comprehensions and I think the
> examples are pretty self-explanatory:
> 
> if not isinstance(x, str) for any x in args:
>   raise TypeError, "arguments must be of type str"

for x in args:
    if not isinstance(x, str):
        raise TypeError, "arguments must be of type str"

> valid = i>0 for every i in vector

valid = True
for i in vector:
    if i <= 0:
        valid = False
        break

> if dict2.has_key(k) for any k in dict1:
>   ...

for k in dict1:
    if dict2.has_key(k):
        break

Other than saving a few lines at the expense of code that is "denser"
and (I believe) harder to understand at a glance, what advantage 
do these constructs have?  Can you post an example where the improvement
is clear and overwhelmingly convincing?

-Peter



More information about the Python-list mailing list