Why does string formatting in Python silently ignore inconsistent arguments with class instances?

Typically, formatting strings in the style of old Python complains if the number of placeholders in the string does not match the number of arguments passed:

>>> 'no.placeholders.here' % 'test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

However, when the argument passed is an instance of a user-defined class, it silently ignores it instead:

>>> class Test(object): pass
>>> 'no.placeholders.here' % Test()
'no.placeholders.here'

This behavior seems inconsistent and has led to some complex errors. Why does the type of format argument matter for this error?

+4
source share
1 answer

That is why %formatting is old

% - , . % :

  • ,
  • tuple dict . list s, set Model s. tuple dicts.

, - . , , (, ).

, 0 , 1, tuple , . , "undefined ".

( ).

, str.format.

+3

All Articles