How to find the type ...

Martin Christensen martin.sand.christensen at gmail.com
Fri Dec 9 12:46:01 EST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>>> "Lad" == Lad  <python at hope.cz> writes:
Lad> How can I find out in Python whether the operand is integer or a
Lad> character and change from char to int ?

In Python, the canonical way of doing this would be to simply assume
that the argument can be converted to an integer and catch any errors
that occur:

def f(x):
    try:
        x = int(x)
    except ValueError:
        # It's a non-number string.
        do stuff
    except TypeError:
        # It's neither a number nor a string.
        do some other stuff

Martin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAkOZwtkACgkQYu1fMmOQldXEzACgqdDVvx29UBVSIfQWnGRiAAk9
xPsAn0yN5jWrUN+6SKIHdwtILRBVyQwR
=HZQq
-----END PGP SIGNATURE-----



More information about the Python-list mailing list