How to find the type ...

L

Lad

Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.
 
M

Magnus Lycka

Lad said:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?

I'm not sure what you mean by "character" in a Python context.
A string? "i = int(i)" will make sure both 5 and "5" are used
as 5, and "five" will be rejected with a ValueError.
.... i = int(x)
.... print i, type(i)
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
ValueError: invalid literal for int(): infinity
 
S

Steve Holden

Lad said:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.
Easiest would just be to apply the int() type function to whatever you
have and trap any resulting exception.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in getInt
ValueError: getInt called with non-integer value

Unfortunately we should also consider:

Traceback (most recent call last):
File "<stdin>", line 1, in ?

which may or may not be what you want.

regards
Steve
 
S

Steven Bethard

Lad said:
How can I find out in Python whether the operand is integer or a
character and change from char to int ?

Python doesn't have a separate character type, but if you want to
convert a one-character string to it's ASCII number, you can use ord():
(65, 122)

The answer to your first question is that you probably don't want to.
You probably want two separate functions, one that takes an integer and
one that takes a character. What's your actual function look like?

STeVe
 
X

Xavier Morel

Lad said:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.
You may want to try the "type" command.
And there is no character type in cPython (unless you're using ctypes
that is)

There is not much point though, you can use the "int" construct on your
expression, Python'll try to convert the expression to an integer by
itself (and throw an exception if it can't)

Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): e
You can even do base conversions with it:

Traceback (most recent call last):
File "<pyshell#7>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): 0xe
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
S

Scott David Daniels

Xavier said:
> You can even do base conversions with it:
> Traceback (most recent call last):
> File "<pyshell#7>", line 1, in -toplevel-
> int(a)
> ValueError: invalid literal for int(): 0xe
> 14
Or even say "look to the text for a clue about the base":
10
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,365
Members
48,050
Latest member
LucieDemps

Latest Threads

Top