Bug or intended behavior?

bob gailer bgailer at gmail.com
Thu Jun 15 09:37:52 EDT 2017


Sending this to docs in hopes of improving documentation of % formatting 
and operator precedence. Perhaps add "see 6:16 Operator precedence."

On 6/3/2017 5:59 PM, Sean DiZazzo wrote:
> On Friday, June 2, 2017 at 10:46:03 AM UTC-7, bob gailer wrote:
>> On 6/2/2017 1:28 PM, Jussi Piitulainen wrote:
>>> sean.dizazzo at gmail.com writes:
>>>
>>>> Can someone please explain this to me?  Thanks in advance!
>>>>
>>>> ~Sean
>>>>
>>>>
>>>> Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)
>>>> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>>> 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'
>>> The per cent operator has precedence over minus. Spacing is not
>>> relevant. Use parentheses.
>>
>> In other words "foo %s" % 1 is executed, giving "1". Then "1"-2 is
>> attempted giving the error.
>> Also: If there is more than one conversion specifier the right argument
>> to % must be a tuple.
>> I usually write a tuple even if there is only one conversion specifier -
>> that avoids the problem
>> you encountered and makes it easy to add more values when you add more
>> conversion specifiers.
>>
>> print "foo %s" % (1-2,)
>>
>> Bob Gailer
> I get what it's doing, it just doesn't make much sense to me.  Looking at operator precedence, I only see the % operator in regards to modulus.  Nothing in regards to string formatting.  Is it just a side effect of the % being overloaded in strings?   Or is it intentional that it's higher precedence...and why?
The documentation is unfortunately flawed. If you look at the .chm file 
in the docs folder under the python installation 
(c:\python35\docs\python351.chm as an example, and enter % in the index, 
you will see:
%
   operator
% formatting
% interpolation
note that modulo is missing!

double-click operator to see the numeric operator
double-click % formatting to see the string "unique built-in operation" 
aka interpolation.
See under section 6:16 Operator precedence:
   "[5] The % operator is also used for string formatting; the same 
precedence applies."

Maybe I'm making too big a deal of it.  It just doesn't 'feel' right to me.

Unfortunately "feel" is not a good guide to understanding programming 
languages.
COBOL and Assembler use MOVE when it is actually COPY!

Slight OT digression: The language that is best for me is APL in which 
there is no operator precedence to worry about. Execution is strictly 
right-to-left. () are used when the order of evaluation needs to be 
altered. I recall one person telling me that "right-to-left" was not 
natural. He preferred "left-to-right" as in FORTRAN. So I considered the 
FORTRAN statement A = B + C * D. Turns out that A * B happens first, 
then C + then A =. Sure looks right-to-left to me!



More information about the Python-list mailing list