Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the canonical way to check for type in Python?

Tags:

python

types

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

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

like image 439
Herge Avatar asked Sep 30 '08 11:09

Herge


People also ask

How do you check if a variable is a certain type in Python?

Use the type() Function to Check Variable Type in Python To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

How do you check if a type is a string Python?

To check if a variable contains a value that is a string, use the isinstance built-in function. The isinstance function takes two arguments. The first is your variable. The second is the type you want to check for.

What is type Command in Python?

The type() function returns the type of the specified object.

How do you check if an object is an instance of a class Python?

Using isinstance() function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object. For example, isinstance(x, int) to check if x is an instance of a class int .


2 Answers

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str): 

To check if the type of o is exactly str (exclude subclasses):

if type(o) is str: 

The following also works, and can be useful in some cases:

if issubclass(type(o), str): 

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

One more note: in this case, if you're using Python 2, you may actually want to use:

if 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). Note that basestring no longer exists in Python 3, where there's a strict separation of strings (str) and binary data (bytes).

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

if isinstance(o, (str, unicode)): 
like image 117
Fredrik Johansson Avatar answered Oct 13 '22 05:10

Fredrik Johansson


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...except to use the object's methods the way you want to use them. So if your 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.

like image 33
Dan Lenski Avatar answered Oct 13 '22 07:10

Dan Lenski