Python and Ruby

bartc bartc at freeuk.com
Tue Feb 2 16:23:37 CET 2010


Jonathan Gardner wrote:

> One of the bad things with languages like perl and Ruby that call
> without parentheses is that getting a function ref is not obvious. You
> need even more syntax to do so. In perl:
>
>  foo();       # Call 'foo' with no args.
>  $bar = foo;  # Call 'foo; with no args, assign to '$bar'
>  $bar = &foo; # Don't call 'foo', but assign a pointer to it to '$bar'
>               # By the way, this '&' is not the bitwise-and '&'!!!!
>  $bar->()     # Call whatever '$bar' is pointing at with no args
>
> Compare with python:
>
>  foo()       # Call 'foo' with no args.
>  bar = foo() # 'bar' is now pointing to whatever 'foo()' returned
>  bar = foo   # 'bar' is now pointing to the same thing 'foo' points to
>  bar()       # Call 'bar' with no args
>
> One is simple, consistent, and easy to explain. The other one requires
> the introduction of advanced syntax and an entirely new syntax to make
> function calls with references.

If you get rid of the syntax specific to Perl, then having to explicitly
obtain a function reference, or to dereference the result, is not such a big
deal:

 foo          # Call 'foo' with no args.
 bar = foo    # Call 'foo; with no args, assign to 'bar'
 bar = &foo   # Don't call 'foo', but assign a pointer to it to 'bar'
 bar^         # Call whatever 'bar' is pointing at with no args

(Here I use ^ instead of -> to dereference.)  Compared with Python, it saves
3 lots of (), but needs & and ^ added. Still a net saving.

> One of the bad things with languages like perl and Ruby that call
> without parentheses is that getting a function ref is not obvious.

I'd say that having this "&" symbol in front of "foo" makes it more obvious
than just foo by itself. But I agree not quite as clean.

Another thing is that you have to know whether "bar" is a function, or a 
function ref, and use the appropriate syntax. Sometimes this is helpful, 
sometimes not.


-- 
Bartc





More information about the Python-list mailing list