How to convert a "long in a string" to a "long"?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Nov 18 22:57:23 EST 2005


On Fri, 18 Nov 2005 17:49:50 +0000, Leif K-Brooks wrote:

> Sion Arrowsmith wrote:
>> Steven Bethard  <steven.bethard at gmail.com> wrote:
>> 
>>>ondekoza at gmail.com wrote:
>>>
>>>>s = long("0xffffffffL")
>>>>ValueError: invalid literal for long(): 0xffffffffL
>>>>
>>>>>>int("0xffffffff", 0)
>>>
>>>4294967295L
>> 
>> So why does the base argument to int() (or long()) default to
>> 10 and not 0?
> 
> Because it's designed for numbers normal people provide, not for numbers
> programmers provide. Normal people see 0123 as being equal to 123, not 83.

The base arguments to int() and long() default to base 10 because base 10
is used by just about all people and cultures in the world. Leading zeroes
are mathematically meaningless: 0123 means 0*base**3 + 1*base**2 +
2*base**1 + 3*base**0, which is identical to 123 no matter what base you
choose.

Interpreting 0123 in octal is a sop to programmers who want/need
compatibility to the C bug that changes the meaning of numeric literals
according to the presence or absence of a leading zero. Alas I suspect
that this particular piece of illogic is too ingrained now to ever
eradicate.


-- 
Steven.




More information about the Python-list mailing list