OcelotcR
OcelotcR

Reputation: 171

Type checking python?

Is there a way to check from a users input what type it is? and do something like so:

if input == str:
   do this
elif input == int:
   do this
else:
   do this

Upvotes: 4

Views: 1294

Answers (5)

Andrew Gorcester
Andrew Gorcester

Reputation: 19983

If you mean keyboard input, then all input is a string. Even if the user types in a number like "88", the result will be the string "88" and not the integer 88. You can, however, use this pattern to tell if the string contains digits and nothing else:

if input.isdigit():
    print "input is a number"
else:
    print "input is not a number"

Even better is to cast the input to a number, which will handle input like "50.0" or "-10" as mentioned in the comments (if decimals are valid, then use float() or Decimal() instead of int() here):

try:
    number = int(input)
except ValueError:
    print "input is not a number"

If you aren't referring to keyboard input, then there are various ways to check types, but in general the best thing to do is to simply use the input as if it were valid and catch exceptions that will arise if it is not valid. That way, you define your input requirements in terms of the actual methods and attributes of the object and not simply its nominal class (this is "duck typing").

Upvotes: 1

mgilson
mgilson

Reputation: 310137

You can use isinstance to check the type of something -- But you really probably don't want to do this ...

This also depends a lot on how you're getting the input from the user. If you're using raw_input, then it is always a string and you'll need to try to convert it to whatever type you want:

try:
   x = int(input):
except ValueError:
   try:
       x = float(input):
   except ValueError:
       print "No recognizable type!"

One basic principle of python is duck-typing. If an object looks like a duck, sounds like a duck and smells like a duck, then it's a duck (provided you only care what the object looks, smells and sounds like). Ultimately, in python it's best to try something to see if it is a "duck". If it isn't a duck, then you catch the Exception and handle it (if you know how). This is the primary reason you don't see people using isinstance very much in their code and the reason I warn against using it.

Upvotes: 5

James Sapam
James Sapam

Reputation: 16940

isinstance(input, type)

type built-in function also can do the same but isinstance() built-in function is recommended for testing the type of an object.

Upvotes: 0

Penner
Penner

Reputation: 29

you can use isinstance http://docs.python.org/2/library/functions.html#isinstance

example

isinstance('hello', str)

returns True

Upvotes: 1

BenDundee
BenDundee

Reputation: 4521

I think you're looking for isinstance.


You beat me, but mine comes with a link:

http://docs.python.org/2/library/functions.html#isinstance


As an aside, I've been test driving PyCharm IDE, and they'll do type checking for you automagically if you assert isinstance() in your code, then it does auto-completions. It also allows you to specify type arguments in the docstring. It then uses this type information to help you with auto completes down the road. Pretty handy...

Upvotes: 0

Related Questions