[Python-Dev] str with base

Alex Martelli aleaxit at gmail.com
Thu Jan 19 03:16:03 CET 2006


On Jan 18, 2006, at 11:09 AM, Brett Cannon wrote:

> On 1/18/06, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
>>>> I'd propose bin() to stay in line with the short abbreviated names.
>>>
>>> There has been some previous discussion about removing hex()/oct()
>> from
>>> builtins for Python 3.0, IIRC.  I sure don't think bin() belongs
>> there.
>>
>> Perhaps introduce a single function, base(val, radix=10,  
>> prefix=''), as
>> a universal base converter that could replace bin(), hex(), oct(),  
>> etc.
>>
>> That would give us fewer builtins and provide an inverse for all the
>> int() conversions (i.e. arbitrary bases).  Also, it would allow an
>> unprefixed output which is what I usually need.
>
> +1.  Differs from Neal's format() function by not magically
> determining the prefix from the radix which I like.

I'm not sure I see the advantage of, say,

print base(x, radix=2, prefix='0b')

versus

print '0b'+base(x, radix=2)

IOW, if the prefix needs to be explicitly specified anyway, what's  
the advantage of specifying it as an argument to base, rather than  
just string-concatenating it?

Apart from that quibble, the base function appears to cover all the  
use cases for my proposed str-with-base, so, since it appears to  
attract less arguments, I'm definitely +1 on it.


Alex




More information about the Python-Dev mailing list