开发者

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

This question already has answers here: What's the canonical way to check for type in Python? (15 answers) Closed 8 months ago.

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.


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


You just use:

type(varname)

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


make use of isinstance ?

if isinstance(var, int):

if isinstance(var, str):

if isinstance(var, tuple):


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:


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜