where clause

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Feb 5 13:04:35 EST 2009


This comes after a small discussion in another Python newsgroup.
Haskell supports a where clause, that's syntactic sugar that allows
you to define things like this:

p = a / b
  where
    a = 20 / len(c)
    b = foo(d)

That means:

a = 20 / len(c)
b = foo(d)
p = a / b

I don't know how much good this syntax can be in my Python programs,
probably I have to use it some time to judge.

In the Python shell you usally have to use a bottom-up style of
programming, while a where may allow you a more top-down too. I can
enjoy this.

Compared to Haskell a possible problem may from mutability, in Haskell
often the order of the operations isn't important (it's only/mostly
significant during I/O), while in Python is generally important.

The interpreter has to look ahead, to use such 'where', because the
point of 'where' is to allow the programmer with such inversions.

In Python you probably want to put a : after where.
But that Haskell syntax also enjoys having where too indented, this is
less easy (or impossible?) to mix with the usual Python syntax.

Bye,
bearophile



More information about the Python-list mailing list