Python and Ruby

Jonathan Gardner jgardner at jonathangardner.net
Tue Feb 2 23:01:01 CET 2010


On Feb 1, 6:36 pm, John Bokma <j... at castleamber.com> wrote:
> Jonathan Gardner <jgard... 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...
>

I rest my case. I've been programming perl professionally since 2000,
and I still make stupid, newbie mistakes like that.

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

Except for the following symbols and combinations, which are entirely
new and different from the $@% that you have to know just to use
arrays and hashes.

\@, ->[ ]
\%, ->{ }
\&, ->( )

By the way:
* How do you do a hashslice on a hashref?
* How do you invoke reference to a hash that contains a reference to
an array that contains a reference to a function?

Compare with Python's syntax.

# The only way to assign
a = b

# The only way to call a function
b(...)

# The only way to access a hash or array or string or tuple
b[...]

> Also, it helps to think of
>
> $ as a thing
> @ as thingies indexed by numbers
> % as thingies indexed by keys
>

I'd rather think of the task at hand than what each of the funky
symbols on my keyboard mean.



More information about the Python-list mailing list