Python and Ruby

sjdevnull at yahoo.com sjdevnull at yahoo.com
Wed Feb 3 00:07:47 CET 2010


On Feb 2, 5:01 pm, Jonathan Gardner <jgard... at jonathangardner.net>
wrote:
> 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

>>> locals().__setitem__('a', 'b')
>>> print a
b

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

>>> def b(a):
...    print a*2
>>> apply(b, (3,))
6

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

>>> b={}
>>> b[1] = 'a'
>>> print b.__getitem__(1)
a





More information about the Python-list mailing list