[Tutor] Doing this in reverse?

Marc Tompkins marc.tompkins at gmail.com
Sun Sep 14 21:34:59 CEST 2008


On Sun, Sep 14, 2008 at 4:48 AM, Kent Johnson <kent37 at tds.net> wrote:

> The docs say, "If radix is zero, the proper radix is guessed based on
> the contents of string; the interpretation is the same as for integer
> literals."
>
> 'guessed' is not really a very good choice of words; 'determined'
> might be better. The last part of the sentence is the clue.
>

You're right - if it had said "determined", I would have thought to insert a
"0x" at the beginning of my test string.
Since it said "guessed", it didn't occur to me to do that, with the
following result:

>> int("FF", 0)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 0: 'FF'
which seemed to say to me that the function is trying to interpret the
string as base 0: what the heck would that mean, anyway?  Is that not just a
particularly elaborate version of division by zero?

I had written a short passage reviling this as a useless feature, when it
struck me that there actually is a use case for it: if you're parsing a text
file (or perhaps the Windows registry?) full of numeric values in arbitrary
encodings.  You know - on one line there's a value expressed in decimal,
then something in octal, something in binary, something in hex.  If I ever
run into a file like that - and so far I never have - I won't have to write
a custom parser.  Hurrah!

-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080914/f48ae491/attachment-0001.htm>


More information about the Tutor mailing list