Eric V. Smith added the comment:
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.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue28307>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com