newbie question int to string conversion

Skip Montanaro skip at mojam.com
Sat Oct 2 08:23:17 EDT 1999


    D'Arcy> Does exception handling carry a lot of overhead?  I routinly do
    D'Arcy> things like this.

    D'Arcy>     try: return string.atoi(v)
    D'Arcy>     except:
    D'Arcy>         try: return string.atof(v)
    D'Arcy>         except: return string.strip(v)

    D'Arcy> Of course, this is in a CGI where network lag hides a multitude
    D'Arcy> of sins.  :-)

Exception handling is expensive if you hit the except clause a lot.  If it's
an occasional thing or you are only processing a small number of CGI inputs,
you should be okay.  On the other hand, if you're doing something (bizarre)
like

    v = "abc"
    for i in range(10000):
        try: result = string.atoi(v)
        except:
            try: result = string.atof(v)
            except: result = string.strip(v)

then you should test for valid input before attempting conversion.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list