[Python-ideas] A suggestion for Python 3 vs Python 2
אלעזר
elazarg at gmail.com
Wed Nov 13 21:44:35 CET 2013
2013/11/13 anatoly techtonik <techtonik at gmail.com>:
> I don't know Ruby. Where I can read more about this Ruby's fail?
The Ruby Programming Language, 2.1.6.1: "spaces and method invocations":
"""
Ruby's grammar allows the parentheses around method invocations to be
omitted in certain circumstances.
This allows Ruby methods to be used as if they were statements, which
is an important part of Ruby's elegance. Unfortunately, however, it
opens up a pernicious whitespace dependency. Consider the following
two lines, which differ only by a single space:
f(3+2)+1
f (3+2)+1
The first line passes the value 5 to the function f and then adds 1 to
the result. Since the second line has a space after the function name,
Ruby assumes that the parentheses around the method call have been
omitted.
The parentheses that appear after the space are used to group a
subexpression, but the entire expression (3+2)+1 is used as the method
argument. If warnings are enabled (with -w), Ruby issues a warning
whenever it sees ambiguous code like this.
The solution to this whitespace dependency is straightforward:
* Never put a space between a method name and the opening parenthesis.
* If the first argument to a method begins with an open parenthesis,
always use parentheses in the method invocation. For example, write
f((3+2)+1).
* Always run the Ruby interpreter with the -w option so it will warn
you if you forget either of the rules above!
"""
I think avoiding this problem is better than dodging it and have a
special warning, Even if print does not return meaningful value in the
first place.
>
>>>>> print(1,2)
>> 1 2
>>>>> print (1,2)
>> (1,2)
>>
>> That's horrible.
>
> They should both map to print as expression, meaning print as a
> function, i.e. to the first one.
So you want Python to behave like this:
>>> xxx = (1,2)
>>> print xxx.count(1)
1
>>> print (1,2).count(1) # or (1).real, since 1.real is an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'count'
That's pretty incosistent and will surprise beginners.
My own feelings is that this feature is the kind of thing that can
make a person leave the language, or never begin using it in the first
place; Things like this are part of the reason I don't use Ruby. And
all this to save keystrokes? If it was part of a bigger feature, like
ML's curried functions syntax, it would have been great - things like:
perr = print sys.stderr
perr "Bad command or file name"
But keystrokes just can't be the reason for introducing such inconsistencies.
Elazar
More information about the Python-ideas
mailing list