Switch statement (was: Lambda going out of fashion)

Discussion in 'Python' started by Skip Montanaro, Dec 23, 2004.

  1. Stephen> {
    Stephen> 'one': lambda x:x.blat(),
    Stephen> 'two': lambda x:x.blah(),
    Stephen> }.get(someValue, lambda x:0)(someOtherValue)

    One thing to remember is that function calls in Python are pretty damn
    expensive. If x.blat() or x.blah() are themselves only one or two lines of
    code, you might find that your "switch" statement is better written as an
    if/elif/else statement. You're making potentially three function calls
    (get(), lambda and x.blat() or x.blah()) to perform what might only be a
    small handful of inline statements. I'll ignore the readability cost of
    your solution vs. an if statement.

    Stephen> So, the questions I am asking are: Is this okay with everyone?

    Sure. I'll adjust.

    Stephen> Does anyone else feel that lambda is useful in this kind of
    Stephen> context?

    It's useful in this sort of context. It will probably always be limited to
    single expressions, which will always leave it a second-class citizen in
    Python. Interestingly enough, lambda in the Lisp world has the same
    limitation, however, since Lisp code is nothing but a series of
    s-expressions, that's not a problem.

    Stephen> Are there alternatives I have not considered?

    I've never seen a situation where if/elif/else wasn't adequate or was less
    clear than the many attempts at switch-like behavior.

    Folks (in general), there is still an open PEP on a switch statement for
    Python. It's been idle since late 2001:

    http://www.python.org/peps/pep-0275.html

    It would help if interested people were to take a look at it and identify
    open issues. If you google for pep 275 you will probably find relevant
    python-dev discussions from the 2001/2002 timeframe. Thomas Wouters' patch
    for the interpreter would also need to be resurrected and brought
    up-to-date. I not longer remember why the PEP stalled.

    Skip
     
    Skip Montanaro, Dec 23, 2004
    #1
    1. Advertising

  2. Skip Montanaro

    rzed Guest

    Skip Montanaro <> wrote in
    news::

    >
    > Stephen> {
    > Stephen> 'one': lambda x:x.blat(),
    > Stephen> 'two': lambda x:x.blah(),
    > Stephen> }.get(someValue, lambda x:0)(someOtherValue)
    >
    > One thing to remember is that function calls in Python are
    > pretty damn expensive. If x.blat() or x.blah() are themselves
    > only one or two lines of code, you might find that your "switch"
    > statement is better written as an if/elif/else statement.
    > You're making potentially three function calls (get(), lambda
    > and x.blat() or x.blah()) to perform what might only be a small
    > handful of inline statements. I'll ignore the readability cost
    > of your solution vs. an if statement.
    >
    > Stephen> So, the questions I am asking are: Is this okay
    > with everyone?
    >
    > Sure. I'll adjust.
    >
    > Stephen> Does anyone else feel that lambda is useful in this
    > kind of Stephen> context?
    >
    > It's useful in this sort of context. It will probably always be
    > limited to single expressions, which will always leave it a
    > second-class citizen in Python. Interestingly enough, lambda in
    > the Lisp world has the same limitation, however, since Lisp code
    > is nothing but a series of s-expressions, that's not a problem.
    >
    > Stephen> Are there alternatives I have not considered?
    >
    > I've never seen a situation where if/elif/else wasn't adequate
    > or was less clear than the many attempts at switch-like
    > behavior.
    >
    > Folks (in general), there is still an open PEP on a switch
    > statement for Python. It's been idle since late 2001:
    >
    > http://www.python.org/peps/pep-0275.html
    >
    > It would help if interested people were to take a look at it and
    > identify open issues. If you google for pep 275 you will
    > probably find relevant python-dev discussions from the 2001/2002
    > timeframe. Thomas Wouters' patch for the interpreter would also
    > need to be resurrected and brought up-to-date. I not longer
    > remember why the PEP stalled.
    >


    It seems to me that it was regarded as misguidod.

    --
    rzed
     
    rzed, Dec 23, 2004
    #2
    1. Advertising

  3. On Thu, 23 Dec 2004 19:57:27 GMT, rumours say that rzed
    <> might have written:

    [Why did PEP 275 stall?]

    >It seems to me that it was regarded as misguidod.


    QOTPE +1

    (PE=Python Era)

    Oncoming Python book: "Hitchhiker's Guido to the Python Language"
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...
     
    Christos TZOTZIOY Georgiou, Jan 3, 2005
    #3
    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. Stephen Thorne

    Lambda going out of fashion

    Stephen Thorne, Dec 23, 2004, in forum: Python
    Replies:
    63
    Views:
    1,186
    Craig Ringer
    Dec 28, 2004
  2. Roman Suzi
    Replies:
    13
    Views:
    614
    Bengt Richter
    Jan 7, 2005
  3. Replies:
    21
    Views:
    1,068
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    426
    Alexey Smirnov
    Jan 29, 2009
  5. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    186
    Sean O'Halpin
    Mar 30, 2009
Loading...

Share This Page