Python and Ruby

Jonathan Gardner jgardner at jonathangardner.net
Tue Feb 2 17:06:47 EST 2010


On Feb 2, 7:23 am, "bartc" <ba... at freeuk.com> wrote:
> 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.
>

On one shoulder, a demon taunts the programmer: "Ohmygosh, you can
save three keystrokes if you introduce an entirely new syntax with odd
squiggles that make no pronounceable sound in the English language!
Perhaps one day, you can program APL in Python!"

The angel that sits on the other shoulder says, "Alas, poor
programmer, one day, you'll have to read that and understand it. And
heaven help us when we hire a poor college graduate to maintain the
code we wrote five years ago. Or worse, when that poor college
graduate writes code and expects us to read it!"

Thankfully, Guido has banished that demon from the realm of Python a
long time ago.


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

Thankfully, in Python, everything is a ref, so everything is
consistent.



More information about the Python-list mailing list