Message277694
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. |
|
Date |
User |
Action |
Args |
2016-09-29 09:42:27 | eric.smith | set | recipients:
+ eric.smith, serhiy.storchaka |
2016-09-29 09:42:27 | eric.smith | set | messageid: <1475142147.68.0.292624671492.issue28307@psf.upfronthosting.co.za> |
2016-09-29 09:42:27 | eric.smith | link | issue28307 messages |
2016-09-29 09:42:27 | eric.smith | create | |
|