where clause

Albert Hopkins marduk at letterboxes.org
Thu Feb 5 20:30:11 CET 2009


On Thu, 2009-02-05 at 10:04 -0800, bearophileHUGS at lycos.com wrote:
> 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.




More information about the Python-list mailing list