Bug or intended behavior?

Michael Torrie torriem at gmail.com
Mon Jun 5 16:27:30 EDT 2017


On 06/05/2017 02:05 PM, Marko Rauhamaa wrote:
> Michael Torrie <torriem at gmail.com>:

>> On 06/05/2017 01:26 PM, Marko Rauhamaa wrote:
>>> Interestingly, however, Python hasn't extended that principle to the
>>> expression syntax. You could have:
>>>
>>>    >>> 1 + 2*3
>>>    7
>>>    >>> 1+2 * 3
>>>    9
>>
>> And thankfully they didn't. Because it wouldn't make sense to do so.
>>
>> Having whitespace indenting mimics how one thinks of blocks of code
>> and how one might wright it in pseudocode. For expressions, however,
>> no one thinks or writes pseudo-equations without considering the order
>> of operations.
> 
> No-one? That's what tripped up the original poster.

I'm referring to your contrived example with only addition and
multiplication and basic order of operations (multiplication and
division before addition and subtraction).  Everyone knows what
parenthesis mean. No one uses spacing in a manner like you suggest, even
for writing formulas on napkins.  That's my point.  Comparing Python's
whitespace formatting to a theoretical replacing of parenthesis with
white spacing is non sequitor, given the hundreds of years of order of
operation.  I don't see how anyone could seriously claim otherwise.

The precedence of operators beyond the simple basics is not so set in
stone of course, and varies even between programming languages.  I see
no easy way around this other than to encourage new programmers to a)
read the basic documentation on expressions, and b) understand how to
interpret tracebacks and how to break things down and try them in the REPL.



More information about the Python-list mailing list