how to know if a variable is a tuple, a string or an integer?

Python

Python Problem Overview


I am trying to figure out a type mismatch while adding a string to another string in a concatenate operation.

Basically the error returned is a TypeError (cannot concatenate string and tuple); so I would like to figure out where I assigned a value as tuple instead of string.

All the values that I assign are strings, so I gotta figure out where the tuple is coming from, so I was hoping that there is a way in Python to find out what is contained inside a variable and what type is it.

So far using pdb I was able to check the content of the variables, and I get correctly the values that I would expect; but I would like to know also the type of the variable (by logic, if the compiler is able to raise a type error, it means that it knows what is inside a variable and if it is compatible with the operation to perform; so there must be a way to get that value/flag out).

Is there any way to print out the type of a variable in python?

BTW, I tried to change all my variables to be explicitly strings, but is not feasible to force str (myvar), so I cannot just cast as string type everywhere I use strings.

Python Solutions


Solution 1 - Python

isinstance(obj, tuple)
isinstance(obj, basestring)
isinstance(obj, int)

Solution 2 - Python

You just use:

type(varname)

which will output int, str, float, etc...

Solution 3 - Python

make use of isinstance ?

if isinstance(var, int):

if isinstance(var, str):

if isinstance(var, tuple):

Solution 4 - Python

Please note, should you wanted to check your var type in if statement, the construct if type(varname) == "tuple": won't work. But these will:

if type(varname) is tuple:
if type(varname) is list:
if type(varname) is dict:
if type(varname) is int:
if type(varname) is str:

Solution 5 - Python

You probably want to test (assuming Python 2.x) using isinstance(obj, basestring). You have the options of using isinstance, type, and calling the attribute __class__, but isinstance is likely to be the one you want here. Take a look at this article for a more thorough treatment of the differences between the three options.

Solution 6 - Python

repr(object) will give a textual description of object, which should show type and value. Your can print or view this in the debugger.

For simple values repr usually returns the same string as you would write the value literally in code. For custom classes it gives the class name and object id, or something else if the class'es

__repr__

is overridden.

Solution 7 - Python

To complement Goujon's answer consider this list of mixed integers and tuples:

for f in fills:
    print (type(f), f)
    if type(f)==int : print "INTEGER!!!"

Generates this output:

(<type 'int'>, 0)
INTEGER!!!
(<type 'tuple'>, (62973, 39058, 25708, 102))
(<type 'int'>, 1)
INTEGER!!!
(<type 'tuple'>, (16968, 12069, 3329, 102))
(<type 'int'>, 2)
INTEGER!!!
(<type 'tuple'>, (24939, 62205, 30062, 102))
(<type 'tuple'>, (32911, 32911, 0, 153))
(<type 'tuple'>, (32911, 0, 0, 153))
(<type 'tuple'>, (32911, 0, 32911, 153))
(<type 'tuple'>, (65535, 0, 0, 153))
(<type 'tuple'>, (0, 0, 65535, 153))
(<type 'tuple'>, (0, 32911, 0, 153))
(<type 'tuple'>, (0, 65535, 65535, 153))

So the secret isn't to test for "int" character string but, rather test for int keyword instead. If you are like me and migrating from the Bash scripting world, using == tests in the Python scripting world will probably be your first step.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser393267View Question on Stackoverflow
Solution 1 - PythonNicolae DascaluView Answer on Stackoverflow
Solution 2 - PythonGabriel RossView Answer on Stackoverflow
Solution 3 - PythonDrake GuanView Answer on Stackoverflow
Solution 4 - PythonGoujonView Answer on Stackoverflow
Solution 5 - PythonDarren YinView Answer on Stackoverflow
Solution 6 - PythonJürgen StrobelView Answer on Stackoverflow
Solution 7 - PythonWinEunuuchs2UnixView Answer on Stackoverflow