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

A

Andrej Mitrovic

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

http://blog.extracheese.org/2010/02/python-vs-ruby-a-battle-to-the-de...

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
point?"

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?
 
J

Jonathan Gardner

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
point?"

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?

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

Aahz

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.

+1 QOTW
 
D

David Cournapeau

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
point?"

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?

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.

cheers,

David
 
L

Lawrence D'Oliveiro

In message <60b1abce-4381-46ab-91ed-
Also, lambda's are expressions, not statements ...

Is such a distinction Pythonic, or not? For example, does Python distinguish
between functions and procedures?
 
L

Lawrence D'Oliveiro

In message
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.)

Didn’t it have source line numbers in it?

What more do you need?
 
B

Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :
In message <60b1abce-4381-46ab-91ed-


Is such a distinction Pythonic, or not?

Python is (by design) a statement-based language, so yes, this
distinction is pythonic !-)
 
J

John Bokma

Jonathan Gardner said:
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.

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"; };
$anon->();
'

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

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 ;-).
 
C

cjw

Lawrence D'Oliveiro a écrit :

Python is (by design) a statement-based language, so yes, this
distinction is pythonic !-)
Aren't lambda forms better described as function?

Colin W.
 
T

Terry Reedy

Aren't lambda forms better described as function?

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
 
T

Terry Reedy

Is this a function?

lambda : None

What about this?

lambda : sys.stdout.write("hi there!\n")

To repeat: Python lambda expressions evaluate to function objects
identical, except for .__name__ attribute, to the equivalent def statememnt.
<class 'function'>
 
S

Steven D'Aprano

Is this a function?

lambda : None

What about this?

lambda : sys.stdout.write("hi there!\n")

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

Jonathan Gardner

Jonathan Gardner said:
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.

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"; };
$anon->();
'

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

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.

$ perl -e '$a = sub () {die "it may have been javascript, but"}; $b =
sub () {die "I am pretty sure it was perl"}; $b->()'
 
J

Jonathan Gardner

In message




Didn’t it have source line numbers in it?

What more do you need?

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

Jonathan Gardner

In message <60b1abce-4381-46ab-91ed-



Is such a distinction Pythonic, or not? For example, does Python distinguish
between functions and procedures?

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?)
 
S

Steven D'Aprano

Jonathan Gardner said:
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.

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'; [...]
As you can see, and a line number is generated, and the nice name is
shown.

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

Steven D'Aprano

(What the heck is a procedure, anyway? Is this different from a
subroutine, a method, or a block?)

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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top