where clause

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Feb 5 14:57:34 EST 2009


Albert Hopkins:

> One could imagine this getting "out of hand" e.g.

Yes, any syntax can be abused (your example isn't abusive enough).


>       a = 20 / len(c)
>           where
>               c = p / b
>       try:
>           b = foo(d)
>               where
>                   d = bar()
>       except:
>           b = 0
>
> It also begs the question, should the except: clause be written to
> handle an exception raised in foo() as well as bar()? or should one also
> write a try/except around bar()?

This code:

>       a = 20 / len(c)
>           where
>               c = p / b
>       try:
>           b = foo(d)
>               where
>                   d = bar()
>       except:
>           b = 0

Equals to:

a = 20 / len(p / b)
try:
    b = foo(bar())
except:
    b = 0
p = a / b

So the answer is positive.


> Usually when I'm looking at an identifier (function, class, variable)
> being used, I tend to look *up* to see where it is defined.

Right, the main purpose of where is to change that usual way, if you
want.

Note that where may also be designed to create a new scope (as in
Haskell, I think), that's why I have inlined the bar and p/b.

Bye,
bearophile



More information about the Python-list mailing list