Python and Ruby

John Bokma john at castleamber.com
Mon Feb 1 21:36:44 EST 2010


Jonathan Gardner <jgardner at jonathangardner.net> writes:

> One of the bad things with languages like perl

FYI: the language is called Perl, the program that executes a Perl
program is called perl.

> 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 '&'!!!!

It should be $bar = \&foo 
Your example actually calls foo...

[..]

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

The syntax follows that of referencing and dereferencing:

$bar = \@array;       # bar contains now a reference to array
$bar->[ 0 ];          # first element of array referenced by bar
$bar = \%hash;        # bar contains now a reference to a hash
$bar->{ key };        # value associated with key of hash ref. by bar
$bar = \&foo;         # bar contains now a reference to a sub
$bar->( 45 );         # call sub ref. by bar with 45 as an argument

Consistent: yes. New syntax? No.

Also, it helps to think of

$ as a thing
@ as thingies indexed by numbers
% as thingies indexed by keys

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development



More information about the Python-list mailing list