int() should be extended?

Padraig Brady Padraig at Linux.ie
Mon Oct 21 12:23:22 EDT 2002


Xiao-Qin Xia wrote:
> Hi, there,
> 
> int() can convert some string into integer, except a string from hex():
> """
> 
>>>>int("41")
>>>
> 41
> 
>>>>int("041")
>>>
> 41
> 
>>>>int("0x41")
>>>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: invalid literal for int(): 0x41
> 
>>>>exec "c = 0x41"
>>>>c
>>>
> 65
> """
> since use exec is not so good a habit, is there any other way to convert 
> "0x41" to 65 (0x41)?, or int() should be extened to do this job?

In general the way to do it is exec or
eval like:

myint=int(eval(mystring))

I guess having int() supporting 8000 08000 0x8000 8e3 ....
would be silly. That logic is already loaded so you
might as well call it (using eval).

Pádraig.




More information about the Python-list mailing list