Q: Feature Wish: "%" Extension

anonymous no at spam.net
Fri Nov 2 19:55:19 EST 2001


I've been using python for file format translations and test/interface
hardware control applications for a few years. I frequently have to
perform conversions between ASCII representations of numbers and, well,
numbers. Python thoughtfully provides the atoi()/int() functions for
string->number conversions for the radices I use most: 2, 8, 10, 16. And
since many of the fields I have to manipulate are 32 bits and wider, I'm
very grateful for the merge of the int and long types.

For number->string conversion (see it coming yet?), the handy overloaded
"%" operator handles radices 8, 10, and 16 -- with padding,
zero-filling, prefixing (e.g. "0x"). But every time I need to convert a
number to _binary_, I have to call a function of my own to produce the
string, and then intermingle that string with other numbers in the final
"%" format I originally intended. It's awkward and slow.

Here it comes: Am I the only Python user that needs to format numbers to
and from binary, or has anyone else ever wished for a %b format to be
added to the "%" operator? Pushed into C it'd be much faster, and could
handle all the implicit type conversions and formatting flags similar to
those of %d and friends. Treat negative and float numbers under the same
convention as for %x and %o.

I've resisted asking for this core language enhancement before, but with
the recent additions of rawstrings (quietly preferred to see a
distinction between ' and " handle this, but understand the trade-off),
unicode strings, and floor division, I think this proposal seems
comparatively pretty tame in impact to the aesthetics of Python :-).

If this has been shot down before, I'm not aware of it; not trying to be
a pain.

							twv

PS: I'm still dumbfounded why Python's division operator must behave
differently than that in php, js, java, perl, awk, and C/C++ -- colleges
do not teach BASIC anymore -- as I worry about all of my hardware
interface code and auto-comment-wrapping editor :-)



More information about the Python-list mailing list