Bug or intended behavior?

Chris Angelico rosuav at gmail.com
Mon Jun 5 15:45:29 EDT 2017


On Tue, Jun 6, 2017 at 5:26 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Peter Pearson <pkpearson at nowhere.invalid>:
>
>> On Fri, 2 Jun 2017 10:17:05 -0700 (PDT), sean.dizazzo at gmail.com wrote:
>> [snip]
>>>>>> print "foo %s" % 1-2
>>> Traceback (most recent call last):
>>>   File "<stdin>", line 1, in <module>
>>> TypeError: unsupported operand type(s) for -: 'str' and 'int'
>>
>> Others have already pointed out that you're assuming the
>> wrong precedence:
>>
>> Say
>>     "foo %s" % (1-2)
>> not
>>     ("foo %s" % 1) - 2
>
> Python's killer feature is the dependence on indentation, that is,
> visual grouping. If they look like they belong together, they belong
> together.
>
> Interestingly, however, Python hasn't extended that principle to the
> expression syntax. You could have:
>
>    >>> 1 + 2*3
>    7
>    >>> 1+2 * 3
>    9

That's all very well (and cute) for situations with just two
operators. What if you had four, and wanted to use whitespace to
represent precedence (or the lack of it to represent parentheses, if
you prefer)? Do you need to over-space the line?

ChrisA



More information about the Python-list mailing list