How to compare type of an object in Python?
Asked Answered
R

15

236

Basically I want to do this:

obj = 'str'
type ( obj ) == string

I tried:

type ( obj ) == type ( string )

and it didn't work.

Also, what about the other types? For example, I couldn't replicate NoneType.

Romine answered 1/4, 2009 at 23:3 Comment(2)
This works type(obj) == strYammer
str is a class whereas string can be a variable but it has to be defined first. So, unless you have defined what the word "string" is, it is just text. For e.g.: string = 5 print(type(string)) //this will give the class/dtype of the variable, which, in this case was intEddins
C
330
isinstance()

In your case, isinstance("this is a string", str) will return True.

You may also want to read this: http://www.canonical.org/~kragen/isinstance/

Chenille answered 1/4, 2009 at 23:7 Comment(2)
I'd say you (the OP) should definitely read the referenced link, which gives plenty of details of why checking the type of an object is usually a bad idea, and what you probably should be doing instead.Wimer
you should use basestr, not str. otherwise you will not pick unicode. (although for 3.x I think str is the basestr)Liger
A
54

First, avoid all type comparisons. They're very, very rarely necessary. Sometimes, they help to check parameter types in a function -- even that's rare. Wrong type data will raise an exception, and that's all you'll ever need.

All of the basic conversion functions will map as equal to the type function.

type(9) is int
type(2.5) is float
type('x') is str
type(u'x') is unicode
type(2+3j) is complex

There are a few other cases.

isinstance( 'x', basestring )
isinstance( u'u', basestring )
isinstance( 9, int )
isinstance( 2.5, float )
isinstance( (2+3j), complex )

None, BTW, never needs any of this kind of type checking. None is the only instance of NoneType. The None object is a Singleton. Just check for None

variable is None

BTW, do not use the above in general. Use ordinary exceptions and Python's own natural polymorphism.

Adriell answered 2/4, 2009 at 0:34 Comment(2)
If you're validating inputs from a DSL, you need all this, even NoneType. What if a parameter can be a str, unicode, or None? isinstance(x, (str, unicode, types.NoneType)) is much cleaner than checking for None. If you're building tools for deferred computation, or if you're about to launch a long or resource-intensive process, it is valuable to catch type errors ahead of time, during some custom validation step. This has been a critical part of almost every scientific computing project I've ever worked on. Out of all dev projects I've seen, more have needed this than have not.Responsion
What about comparing type directly, instead? For example test(t1: Type, t2: Type) -> bool ? What are the best practices and the docs guidelines there?Hoiden
G
44

isinstance works:

if isinstance(obj, MyClass): do_foo(obj)

but, keep in mind: if it looks like a duck, and if it sounds like a duck, it is a duck.

EDIT: For the None type, you can simply do:

if obj is None: obj = MyClass()
Granuloma answered 1/4, 2009 at 23:26 Comment(2)
def distance_from_zero(n): if isinstance(n,int) or isinstance(n,float): return abs(n) else: return "Nope" print distance_from_zero(True) This returns a "1" instead of "Nope". How to get around this ?Grimbald
If you want to use isinstance but check also for None then isinstance(obj, (MyClass, type(None))) works. types.NoneType has been removed from Python 3 so it is not as portable as type(None) to get a reference to NoneType.Moral
I
26

For other types, check out the types module:

>>> import types
>>> x = "mystring"
>>> isinstance(x, types.StringType)
True
>>> x = 5
>>> isinstance(x, types.IntType)
True
>>> x = None
>>> isinstance(x, types.NoneType)
True

P.S. Typechecking is a bad idea.

Indaba answered 1/4, 2009 at 23:19 Comment(0)
M
20

You can always use the type(x) == type(y) trick, where y is something with known type.

# check if x is a regular string
type(x) == type('')
# check if x is an integer
type(x) == type(1)
# check if x is a NoneType
type(x) == type(None)

Often there are better ways of doing that, particularly with any recent python. But if you only want to remember one thing, you can remember that.

In this case, the better ways would be:

# check if x is a regular string
type(x) == str
# check if x is either a regular string or a unicode string
type(x) in [str, unicode]
# alternatively:
isinstance(x, basestring)
# check if x is an integer
type(x) == int
# check if x is a NoneType
x is None

Note the last case: there is only one instance of NoneType in python, and that is None. You'll see NoneType a lot in exceptions (TypeError: 'NoneType' object is unsubscriptable -- happens to me all the time..) but you'll hardly ever need to refer to it in code.

Finally, as fengshaun points out, type checking in python is not always a good idea. It's more pythonic to just use the value as though it is the type you expect, and catch (or allow to propagate) exceptions that result from it.

Marijane answered 2/4, 2009 at 0:1 Comment(1)
For what it's worth, isinstance() is the preferred way of checking types in Python (when you have to do it).Toed
L
12

Use isinstance(object, type). As above this is easy to use if you know the correct type, e.g.,

isinstance('dog', str) ## gives bool True

But for more esoteric objects, this can be difficult to use. For example:

import numpy as np 
a = np.array([1,2,3]) 
isinstance(a,np.array) ## breaks

but you can do this trick:

y = type(np.array([1]))
isinstance(a,y) ## gives bool True 

So I recommend instantiating a variable (y in this case) with a type of the object you want to check (e.g., type(np.array())), then using isinstance.

Libove answered 27/6, 2018 at 15:27 Comment(0)
S
10

You're very close! string is a module, not a type. You probably want to compare the type of obj against the type object for strings, namely str:

type(obj) == str  # this works because str is already a type

Alternatively:

type(obj) == type('')

Note, in Python 2, if obj is a unicode type, then neither of the above will work. Nor will isinstance(). See John's comments to this post for how to get around this... I've been trying to remember it for about 10 minutes now, but was having a memory block!

Shirtwaist answered 1/4, 2009 at 23:6 Comment(1)
Use basestring with isinstance() to get both str and unicode.Marijane
V
8

Use str instead of string

type ( obj ) == str

Explanation

>>> a = "Hello"
>>> type(a)==str
True
>>> type(a)
<type 'str'>
>>>
Volny answered 9/2, 2013 at 10:48 Comment(0)
M
5

It is because you have to write

s="hello"
type(s) == type("")

type accepts an instance and returns its type. In this case you have to compare two instances' types.

If you need to do preemptive checking, it is better if you check for a supported interface than the type.

The type does not really tell you much, apart of the fact that your code want an instance of a specific type, regardless of the fact that you could have another instance of a completely different type which would be perfectly fine because it implements the same interface.

For example, suppose you have this code

def firstElement(parameter):
    return parameter[0]

Now, suppose you say: I want this code to accept only a tuple.

import types

def firstElement(parameter):
    if type(parameter) != types.TupleType:
         raise TypeError("function accepts only a tuple")
    return parameter[0]

This is reducing the reusability of this routine. It won't work if you pass a list, or a string, or a numpy.array. Something better would be

def firstElement(parameter):
    if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
        raise TypeError("interface violation")
    return parameter[0]

but there's no point in doing it: parameter[0] will raise an exception if the protocol is not satisfied anyway... this of course unless you want to prevent side effects or having to recover from calls that you could invoke before failing. (Stupid) example, just to make the point:

def firstElement(parameter):
    if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
        raise TypeError("interface violation")
    os.system("rm file")
    return parameter[0]

in this case, your code will raise an exception before running the system() call. Without interface checks, you would have removed the file, and then raised the exception.

Moreville answered 3/4, 2009 at 0:14 Comment(1)
Thanks for indicating the actual preferred way to check for interfaces. Many of the answers here mention it, but few give examples of what's good instead. It still doesn't answer my personal question directly (I'm trying to separate a list of strings, containing many, meaningful items, from a string, which contains many, not meaningful items. Thanks!Erastes
L
4

I use type(x) == type(y)

For instance, if I want to check something is an array:

type( x ) == type( [] )

string check:

type( x ) == type( '' ) or type( x ) == type( u'' )

If you want to check against None, use is

x is None
Liger answered 2/4, 2009 at 0:10 Comment(7)
huh? why is it a bad idea in general? It's only a bad idea for strings (for pre 3.0) because there are two types of strings, str and unicode. For arrays, it's a good idea imho.Liger
@hasen: it is a bad idea overall. What if I define my own type that behaves like an array but, say, fetches values from a database? Your code will fail with my type for no reason.Delinda
@hasen: read the link canonical.org/~kragen/isinstance of the most voted (+7) answer, by voltronwDelinda
Well, the entire reason (for me atleast) in checking the type is exactly because I want to deal with arrays differently than other types (including types that imitate arrays).Liger
@hasen: well, that's a really bad idea and your design is broken. Code shouldn't behave in different ways depending on the type of the object passed. That is bug-prone and prevents code reuse, so is generally frowned upon.Delinda
You're wrong. I'll give you a concrete example: django has a template rendering shortcut that can accept either a string or an array of strings. Now, both strings and arrays (lists) are iterable, but in this case, the functions needs to differentiate between them.Liger
@hasenj, Django is broken and that's a broken design. There should be two different functions here.Ptosis
M
3

i think this should do it

if isinstance(obj, str)
Mcgann answered 1/4, 2009 at 23:7 Comment(0)
H
2

Type doesn't work on certain classes. If you're not sure of the object's type use the __class__ method, as so:

>>>obj = 'a string'
>>>obj.__class__ == str
True

Also see this article
Type Checking in Python - Siafoo

Heraldic answered 6/4, 2012 at 21:34 Comment(0)
M
2

To get the type, use the __class__ member, as in unknown_thing.__class__

Talk of duck-typing is useless here because it doesn't answer a perfectly good question. In my application code I never need to know the type of something, but it's still useful to have a way to learn an object's type. Sometimes I need to get the actual class to validate a unit test. Duck typing gets in the way there because all possible objects have the same API, but only one is correct. Also, sometimes I'm maintaining somebody else's code, and I have no idea what kind of object I've been passed. This is my biggest problem with dynamically typed languages like Python. Version 1 is very easy and quick to develop. Version 2 is a pain in the buns, especially if you didn't write version 1. So sometimes, when I'm working with a function I didn't write, I need to know the type of a parameter, just so I know what methods I can call on it.

That's where the __class__ parameter comes in handy. That (as far as I can tell) is the best way (maybe the only way) to get an object's type.

Mcbride answered 1/2, 2013 at 5:7 Comment(0)
C
1

You can compare classes for check level.

#!/usr/bin/env python
#coding:utf8

class A(object):
    def t(self):
        print 'A'
    def r(self):
        print 'rA',
        self.t()

class B(A):
    def t(self):
        print 'B'

class C(A):
    def t(self):
        print 'C'

class D(B, C):
    def t(self):
        print 'D',
        super(D, self).t()

class E(C, B):
    pass

d = D()
d.t()
d.r()

e = E()
e.t()
e.r()

print isinstance(e, D) # False
print isinstance(e, E) # True
print isinstance(e, C) # True
print isinstance(e, B) # True
print isinstance(e, (A,)) # True
print e.__class__ >= A, #False
print e.__class__ <= C, #False
print e.__class__ <  E, #False
print e.__class__ <= E  #True
Chondriosome answered 5/5, 2016 at 7:25 Comment(0)
R
0

Because type returns an object, you can access de the name of the object using object.name

Example:

years = 5
user = {'name':'Smith', 'age':20}

print(type(a).__name__) 

output: 'int'

print(type(b).__name__ )

output: 'dict'

Resonator answered 26/9, 2022 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.