where clause

B

bearophileHUGS

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
 
A

Albert Hopkins

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.

I don't like it for the following reasons:

* Flat is better than nested.
* There should be one-- and preferably only one --obvious way to
do it.

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

p = a / b
where
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()?

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

bearophileHUGS

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
 
P

Paul Rubin

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.

In Haskell, "where" is only allowed at the outermost level of a
function definition (including a nested one), not in an arbitrary
expression.
 
R

Rhodri James

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

You'd want to do it with paired keywords, in the manner of try/except,
to avoid utterly breaking Python's syntax conventions. Perhaps
something like this:

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

or even:

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


Effectively you're creating a little local namespace for temporary
variables. I'm not sure it buys you a lot, even as sugar, and I'm
really not convinced by the bypartite form, but it's definitely
possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top