[Tutor] [string.atoi_error, revisionism, and Python 1.0.2]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Mar 22 14:05:38 EST 2004

On Mon, 22 Mar 2004, Tony Cappellini wrote:

> > > I've found this code on the net- I think it was from the Python Cookbook.
> >
> > >          try:
> > >             Celsius=float(string.atoi(CTemp))
> > >          except string.atoi_error:
> > >             print repr(CTemp), "not a numeric value"
> > >          else:
> >
> > This code is very outdated.  Today with a recent python noone would
> > write it like that.

[some text cut]

> What I don't understand is- when I looked in the Python documentation,
> under exceptions, only the built-in exceptions were listed.

Hi Tony,

Yes, the built-in exceptions in:


do not have string.atoi_error, nor is 'string.atoi_error' documented in
the 'string' module.


Interesting.  Let me check something:

>>> import string
>>> string.atoi_error
<class exceptions.ValueError at 0x40071b0c>

Ah!  string.atoi_error is an alias for the standard exception ValueError!

Ok, I think I have a theory of what's happening.  Let's assume for the
moment that that celsius-calculation code was written a long time ago,
since it uses the deprecated 'string' module.  We can go back to Python
1.52's library documentation:


... but that's not far back enough.  In reality, string.atoi_error is a
REALLY OLD exception type from the 1.0.2 days!

I can't even find that version of the documentation on Python.org anymore,
but I did find it on a mirror site:


I think we can now reconstruct a plausible guess what happened: Python
1.0.2 used to have a separate exception for each of the following:

atof_error -- exception of module string
    Exception raised by atof when a non-float string argument is detected.
The exception argument is the offending string.

atoi_error -- exception of module string
    Exception raised by atoi when a non-integer string argument is
detected. The exception argument is the offending string.

atol_error -- exception of module string
    Exception raised by atol when a non-integer string argument is
detected. The exception argument is the offending string.

index_error -- exception of module string
    Exception raised by index when sub is not found. The exception
argument is undefined (it may be a tuple containing the offending
arguments to index or it may be the constant string 'substring not

But this specialization was probably considered a little overboard:
exceptions should capture the exceptional situation itself --- having
three separate exceptions for each of the conversion function is awkward.

There appears to have been an effort to consolidate the exceptions to be a
little more general, and so atof_error, atoi_eror, and atol_error were all
condensed into the exception that we now know as ValueError.  This
consolidation appears to have taken place between Python 1.0.2 and Python
1.5.2, since there's no trace of it in 1.5.2.

> So- the documentation failed to explain what other types of exceptions
> are already included in Python- even if they are in modules, and not the
> built-ins.

I have to assume that 'atoi_error' was left in the string module simply
for compatibility reasons, since there's really no mention of it in any
modern Python docs now.

Anyway, so in summary: your code snippet was really darn ancient, using
very deprecated functions from at least 1994.  Python has changed quite a
bit since then, and it tries to maintain backwards compatibility, which is
why string.atoi_error is still in that module.

But the language also tries to keep folks from using the deprecated
features, either by documenting them as 'deprecated' (as in the case of
string.atoi())... or by not documenting them at all (as in the case of
string.atoi_error).  *grin*

Hope this helps!

More information about the Tutor mailing list