English-like Python

Joe Strout joe at strout.net
Wed Jan 21 10:17:34 EST 2009


Steven D'Aprano wrote:

>>   LogError "Walk has gotten too silly", CurrentTime
>>
>> Here, LogError is a method call that takes two arguments, and
>> CurrentTime is a method call that takes none.
> 
> That seems ambiguous to me. As a non-RealBasic programmer, I can see at 
> least four meanings it could have. Translated into Python, they are:
> 
> LogError("Walk has gotten too silly", CurrentTime)
> LogError("Walk has gotten too silly"), CurrentTime
> LogError("Walk has gotten too silly", CurrentTime())
> LogError("Walk has gotten too silly"), CurrentTime()

It's not ambiguous because RB doesn't have a tuple syntax that looks the 
same as arguments to a function call.  You also can't get a reference to 
a method simply by naming it; naming it invokes it (when you want a 
reference to it instead, you use the "AddressOf" keyword).  So, the 
first and third of your lines above mean the same thing, and are the 
correct (and only possible) interpretation.  The second and fourth are 
also equivalent, but would require the parentheses around the LogError 
parameter since in that case it is a function (i.e. returns a result).

As I said before, I'm not sure how you would apply this to Python, where 
other syntax choices (tuples, function references, etc.) get in the way 
of this idea.  I'm merely pointing out that what Aaron asked for is 
possible without ambiguity, and actually used in at least one real-world 
language.

> But even if RB doesn't have these things, I question that the syntax is 
> "beautiful". Consider some arbitrary method Foo. If you see this:
> 
>     Foo
> 
> Is that legal RB syntax?

You betcha!  For example, the built-in method to play the standard 
system alert sound is:

     Beep

Doesn't get much more readable and syntax-free than that.  Suppose now 
that a beep isn't eloquent enough, and you want the computer to speak 
something out loud instead.  Also easy:

     Speak "Spam, spam, spam, baked beans and spam."

If you're writing a console app, then there's a built in "Print" 
subroutine that puts a string to stdout.  Its usage is:

     Print "Spam, spam, spam, baked beans and spam."

Note that this syntax is exactly like Python's "print" syntax prior to 
3.0, but in RB, it's not a special case -- it's just another method 
call, and you can define your own methods that you invoke exactly the 
same way.

> Maybe yes, maybe no. It all depends on what Foo 
> does. If it returns no result, then it's legal. If it returns a result, 
> it isn't.

Right.  In other words, you can tell just by looking at the call that it 
doesn't return a result.  This is often handy.

> So the question of whether syntax is legal depends, not on the 
> syntactic elements involved, but on the *semantics* of the method 
> (whether or not it returns a result).

But of course.  Any method call is legal only if the form of the call 
matches the method prototype -- if you try to call a function that 
requires 4 parameters, and give it only 3, that's an error too.  I don't 
see how this is different in any important way.

>> Eliminating unnecessary parentheses does a lot to improve the
>> readability of the code IMHO.
> 
> But they're not unnecessary, at least not in Python, they're useful for 
> distinguishing between calling the function and the function itself. 

Yes, quite true in Python.  If there were some other way to distinguish 
between those -- and if tuple syntax didn't look the same as method call 
arguments -- THEN they would be unnecessary.  But those would be very 
substantial changes to Python syntax, and I'm not seriously proposing them.

Best,
- Joe




More information about the Python-list mailing list