Use isinstance to check if o is an instance of str or any subclass of str:
if isinstance(o, str):
To check if the type of o is exactly str, excluding subclasses of str:
if type(o) is str:
See Built-in Functions in the Python Library Reference for relevant information.
Checking for strings in Python 2
For Python 2, this is a better way to check if o is a string:
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. In Python 3, basestring no longer exists since 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)):
Answer from Fredrik Johansson on Stack OverflowWhat's the canonical way to check for type in Python? - Stack Overflow
Can someone explain to me what TYPE_CHECKING does?
Best practice for type checking and assert statement
Why is typechecking bad in Python?
This is a pretty good summary of why it is considered bad form, and what to do instead (i.e. capability checking): http://blog.codekills.net/2011/09/26/checking-types-in-python/
-- edit: for a very simple tldr, it is hard to typecheck, so it is better to just publish the interface and assume your users will conform, or break if they do not.
More on reddit.comUse isinstance to check if o is an instance of str or any subclass of str:
if isinstance(o, str):
To check if the type of o is exactly str, excluding subclasses of str:
if type(o) is str:
See Built-in Functions in the Python Library Reference for relevant information.
Checking for strings in Python 2
For Python 2, this is a better way to check if o is a string:
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. In Python 3, basestring no longer exists since 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)):
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.
I'm doing a tutorial right now and they use it in multiple classes and I have no idea what it does for the program.
I've tried looking through the documentation, but it hasn't helped much and I can't find anyone talking about what it does.
Sorry if this is an googleable question, but maybe I've just been searching the wrong thing. All I can find is general type checking help instead of this specific function or library. Generally when it's used they'll import TYPE_CHECKING from typing and then use it like:
if TYPE_CHECKING:
# import from another file here