where clause

Discussion in 'Python' started by bearophileHUGS@lycos.com, Feb 5, 2009.

  1. Guest

    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
     
    , Feb 5, 2009
    #1
    1. Advertising

  2. On Thu, 2009-02-05 at 10:04 -0800, 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.
     
    Albert Hopkins, Feb 5, 2009
    #2
    1. Advertising

  3. Guest

    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
     
    , Feb 5, 2009
    #3
  4. Paul Rubin Guest

    writes:
    > 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.
     
    Paul Rubin, Feb 5, 2009
    #4
  5. Rhodri James Guest

    On Thu, 05 Feb 2009 18:04:35 -0000, <> wrote:

    > 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.

    --
    Rhodri James *-* Wildebeeste Herder to the Masses
     
    Rhodri James, Feb 6, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee Simpson

    Re: Formatting string in WHERE clause

    Lee Simpson, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,281
    Lee Simpson
    Jul 18, 2003
  2. Jan
    Replies:
    2
    Views:
    1,480
    Mike Treseler
    Dec 16, 2004
  3. bxbxb3

    "else process" clause

    bxbxb3, May 26, 2005, in forum: VHDL
    Replies:
    3
    Views:
    693
    combinational.logic $ soc-ip.com
    May 27, 2005
  4. crazyrdx

    use clause

    crazyrdx, Aug 30, 2005, in forum: VHDL
    Replies:
    1
    Views:
    634
  5. Yaroslav
    Replies:
    2
    Views:
    453
Loading...

Share This Page