This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients eric.smith, serhiy.storchaka
Date 2016-09-29.09:42:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1475142147.68.0.292624671492.issue28307@psf.upfronthosting.co.za>
In-reply-to
Content
There isn't a direct mapping between %-formatting and __format__ format specifiers. Off the top of my head, I can think of at least one difference:

>>> '%i' % 3
'3'
>>> '{:i}'.format(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'i' for object of type 'int'

So you'll need to be careful with edge cases like this.

Also, for all usages of %s, remember to call str() (or add !s):

>>> '%s' % 1
'1'
>>> f'{1:s}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 's' for object of type 'int'
>>> f'{1!s:s}'
'1'

Although that also reminds me of this default alignment difference:
>>> x=0
>>> '%2s' % x
' 0'
>>> f'{x!s:2s}'
'0 '
>>> f'{x!s:>2s}'
' 0'

So, in general, the mapping will be difficult. On the other hand, if you can do it, and provide a function that maps between %-formatting codes and __format__ codes, then that might be a generally useful tool.
History
Date User Action Args
2016-09-29 09:42:27eric.smithsetrecipients: + eric.smith, serhiy.storchaka
2016-09-29 09:42:27eric.smithsetmessageid: <1475142147.68.0.292624671492.issue28307@psf.upfronthosting.co.za>
2016-09-29 09:42:27eric.smithlinkissue28307 messages
2016-09-29 09:42:27eric.smithcreate