osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] PEP 461: Adding % formatting to bytes and bytearray -- Final, Take 2


On 2/23/2014 4:25 PM, Ethan Furman wrote:
> I agree that having only one decimal format code would be nice, or even
> two if the second one did something different, and that three seems
> completely over the top -- unfortunately, Python 3.4 still supports all
> three (%d, %i, and %u). Not supporting two of them would just lead to
> frustration.  There is also no reason to exclude %o nor %x and making
> the programmer reach for oct() and hex().  We're trying to simplify
> %-interpolation, not garner exclamations of "What were they
> thinking?!?"  ;)

There are things that can be done with %o and %x that cannot be done
with oct() and hex(), or at least cannot be done without a terrific
amount of byte munging. For example:

>>> '%#.4x' % 42
'0x002a'

Not sure you'd ever need to do that in a wire protocol, but it's possible.

Since one of the motivators of this feature is to make porting easier,
I'd suggest fully supporting the numeric codes that are supported in 2.7.

I do have some sympathy for the "change your code to a common 2.x-3.x
subset" position. But since 2.7's -3 flag won't (and can't) warn you
when you're doing something with %-formatting that's not support in 3.x,
I think the user-friendliest approach is to support all of the numeric
codes as completely as possible.

Eric.