Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic f

Discussion in 'Python' started by Casey Hawthorne, Feb 16, 2010.

  1. Casey Hawthorne, Feb 16, 2010
    1. Advertisements

  2. Gary's friend Geoffrey Grosenbach says in his blog post (which Gary
    linked to): "Python has no comparable equivalent to Ruby’s do end
    block. Python lambdas are limited to one line and can’t contain
    statements (for, if, def, etc.). Which leaves me wondering, what’s the

    I'm sorry, lambda's do support if's and for's. Also, lambda's are
    expressions, not statements, but you can pass them around, keep them
    in a dictionary if you want to. And if you need more than one line of
    statements, for crying out loud use a def? And who needs those "do-
    end" blocks anyway, trying to turn Python into Pascal?
    Andrej Mitrovic, Feb 16, 2010
    1. Advertisements

  3. I used to think anonymous functions (AKA blocks, etc...) would be a
    nice feature for Python.

    Then I looked at a stack trace from a different programming language
    with lots of anonymous functions. (I believe it was perl.)

    I became enlightened.
    Jonathan Gardner, Feb 17, 2010
  4. Casey Hawthorne

    Aahz Guest

    +1 QOTW
    Aahz, Feb 17, 2010
  5. I think that's a bit of a strawman: the point made by the OP is that
    it enables writing simple DSL easier, and the ruby's community seems
    to value this. They are not advocating using anonymous functions where
    "normal" functions would do.


    David Cournapeau, Feb 17, 2010
  6. In message <60b1abce-4381-46ab-91ed-
    Is such a distinction Pythonic, or not? For example, does Python distinguish
    between functions and procedures?
    Lawrence D'Oliveiro, Feb 17, 2010
  7. In message
    Didn’t it have source line numbers in it?

    What more do you need?
    Lawrence D'Oliveiro, Feb 17, 2010
  8. Aahz a écrit :
    ++1 QOTW !-)

    Had the same problem trying to debug some javascript...
    Bruno Desthuilliers, Feb 17, 2010
  9. Lawrence D'Oliveiro a écrit :
    Python is (by design) a statement-based language, so yes, this
    distinction is pythonic !-)
    Bruno Desthuilliers, Feb 17, 2010
  10. Casey Hawthorne

    John Bokma Guest

    If it was Perl [1], I doubt it. Because line numbers are reported, and
    if that doesn't help you, you can annotate anonymous functions with a
    nick name using

    local *__ANON__ = 'nice name';

    Finding an issue, and not looking for a solution is not called becoming
    enlightened ;-)

    ~$ perl -e '
    use Carp;

    my $anon = sub { local *__ANON__ = "hello, world"; croak "oops"; };

    oops at -e line 4
    main::hello, world() called at -e line 5

    As you can see, and a line number is generated, and the nice name is

    If you generate anonymouse functions on the fly based on parameters, you
    can encode this into the nice name, of course.

    Sadly, often bold statements about a language are made in ignorance.

    [1] perl is the program that executes Perl programs ;-).
    John Bokma, Feb 17, 2010
  11. Casey Hawthorne

    cjw Guest

    Aren't lambda forms better described as function?

    Colin W.
    cjw, Feb 17, 2010
  12. Casey Hawthorne

    Terry Reedy Guest

    They are expressions that evaluate to function objects nearly identical
    to that produced by the def statememt they abbreviate. The only
    difference is the .__name__ attribute.

    Terry Jan Reedy
    Terry Reedy, Feb 17, 2010
  13. Is this a function?

    lambda : None

    What about this?

    lambda : sys.stdout.write("hi there!\n")
    Lawrence D'Oliveiro, Feb 17, 2010
  14. Casey Hawthorne

    Terry Reedy Guest

    To repeat: Python lambda expressions evaluate to function objects
    identical, except for .__name__ attribute, to the equivalent def statememnt.
    <class 'function'>
    Terry Reedy, Feb 17, 2010
  15. Of course they are; the first is a function that takes no arguments and
    returns None, and the second is a function that takes no arguments,
    returns None, and has a side-effect of writing "hi there\n" to stout.

    But I imagine you already know that, so I'm not really sure I understand
    the point of your (rhetorical?) question.
    Steven D'Aprano, Feb 18, 2010
  16. $ perl -e '$a = sub () {die "it may have been javascript, but"}; $b =
    sub () {die "I am pretty sure it was perl"}; $b->()'
    Jonathan Gardner, Feb 18, 2010
  17. I don't know, but I tend to find the name of the function I called to
    be useful. It's much more memorable than line numbers, particularly
    when line numbers keep changing.

    I doubt it's just me, though.
    Jonathan Gardner, Feb 18, 2010
  18. Not to the programmer, no. Callables are callable, no matter what they
    are, and they are all called the same way.

    (What the heck is a procedure, anyway? Is this different from a
    subroutine, a method, or a block?)
    Jonathan Gardner, Feb 18, 2010
  19. Given that it has a nice name, what makes it an anonymous function?

    It seems to me that Perl effectively has three ways of creating
    functions, one anonymous and two named (even if one syntax for creating a
    named function is almost identical to the syntax for creating an
    anonymous function). Once you annotate a function with a nickname, it's
    no different from giving it a name.

    If this is the case, then your answer to "anonymous functions are a PITA"
    is "don't use anonymous functions", which exactly the same answer we'd
    give here in Python land. The only difference is that Perl provides two
    ways of making a named function, and Python only one[1].

    [1] Technically, you can make named functions with the new module and a
    bit of work, so Python has two ways too.
    Steven D'Aprano, Feb 18, 2010
  20. The name is used in Pascal, which probably means it originated from
    Fortran or Algol.

    A subroutine is a generic piece of code which can be re-used by some
    unspecified mechanism (GOSUB in Basic, by calling it in most other
    languages). A function is a subroutine that returns a result, and a
    procedure is a subroutine that doesn't return anything (not even None, or
    the equivalent thereof) and operates entirely by side-effect.
    Steven D'Aprano, Feb 18, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.