tags:

views:

2586

answers:

6

What is the best way to check if a given object is of a given type. How about checking if the object inherits from a given type.

Let's say I have an object o. How do I check if it's an str?

+1  A: 
isinstance(o, str)

Link

Alexander Kojevnikov
A: 

Sorry to be the bad cop, but asking this question is admitting you're not making full use of an object oriented design.

(see also this question about is-a)

xtofl
Polymorphism rules!
S.Lott
+3  A: 

I'm going to be a complete karma whore and post an answer because I just remembered it.

isinstance(o, str) will return true if o is an str or is of a type that inherits from str.

type(o) == str will return true if and only if o is a str. It will return false if o is of a type that inherits from str.

Herge
Of course, this will fail if the object is not an instance of 'str', but of something string-like instead. Like unicode, mmap, UserString or any other user-defined type. The usual approach in Python is not to do typechecks.
Thomas Wouters
You don't have to apologize, it is OK to answer your own question. SO is for the answers, not the karma.
Eli Bendersky
This is very helpful. Because the difference between `isinstance` and `type(var) == type('')` is not clear.
jetxee
+31  A: 

To check if the type of o is exactly str:

type(o) is str
To check if o is an instance of str or any subclass of str (this would be the "canonical" way):
isinstance(o, str)
The following also work, and can be useful in some cases:
issubclass(type(o), str)
type(o) in ([str] + str.__subclasses__())

See Built-in Functions in the Python Library Reference for relevant information.

One more note: in this case, you may actually want to use

isinstance(o, basestring)

because this will also catch Unicode strings (unicode is not a subclass of str; both str and unicode are subclasses of basestring).

Alternatively, isinstance accepts a tuple of classes. This will return True if x is an instance of any subclass of any of (str, unicode):

isinstance(o, (str, unicode))
fredrikj
str.__subclasses__() only returns the direct subclasses of str, and does not do the same thing as issubclass() or isinstance(). (To do that, you would have to recursively call .__subclasses__().
Thomas Wouters
+2  A: 

I think the cool thing about using a dynamic language like python is you really shouldn't have to check something like that.

I would just call the required methods on your object and catch an AttributeError. Later on this will allow you to call your methods with other (seemingly unrelated) objects to accomplish different tasks, such as mocking an object for testing.

I've used this alot when getting data off the web with urllib2.urlopen() which returns a file like object. This can in turn can be passed to almost any method that reads from a file, because is implements the same read() method as a real file.

But I'm sure there is a time and place for using isinstance(), otherwise it probably wouldn't be there :)

Will Harding
+8  A: 

The most Pythonic way to check the type of an object is... not to check it.

Since Python encourages Duck Typing, you should just try to use the object's methods the way you want to use them. So if you're function is looking for a writable file object, don't check that it's a subclass of file, just try to use its .write() method!

Of course, sometimes these nice abstractions break down and isinstance(obj, cls) is what you need. But use sparingly.

Dan
IMHO, the most Pythonic way is to cope with whatever argument which is given. In my code I often cannot know if I recieve an object or an array of objects, and I use type-checking internally to convert a single object to a one-element list.
jetxee