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 is a syntax error in Python?
A Python syntax error is an issue that occurs when Python code is interpreted during execution. Syntax errors are one of three basic types of error, and are almost always fatal because the Python interpreter cannot understand a line of code. Logic errors occur when the code is valid, but the application doesn’t do what the developer intended. Exceptions occur when the Python parser understands a line of code, but the interpreter is unable to execute it during runtime.
Why use Snyk's Python Code Checker?
What does it do? Snyk’s Python Code Checker (Snyk Code) is an AI-powered SAST tool that analyzes Python code for security issues and bugs, delivering real-time feedback within your IDE.
What types of issues are detected? It finds a broad spectrum of bugs (e.g., file I/O corruption, API misuse, null dereference, threading deadlocks, regex DoS, resource leaks) and vulnerabilities (e.g., code injection, SQL injection, weak cryptography, information disclosure).
How is AI implemented? The tool leverages a human-in-the-loop AI model—combining expert-curated rules with advanced ML for semantic, data-flow, and structural code analysis.
Integration capabilities? It integrates seamlessly with your workflow—providing real-time scanning in IDEs and CI/CD, plus PR scanning to enforce security before code merges.
What analysis methods are applied? It applies configuration, semantic, data-flow, and structural analyses to deeply understand code behavior and context.
Why use an AI-powered checker like this? AI enables earlier detection of sophisticated bugs and vulnerabilities that ordinary linters miss—reducing false positives and improving developer efficiency.
Does it support Python dependency scanning? Yes—while Snyk Code focuses on code logic, Snyk Open Source handles dependency scanning, offering comprehensive Python security.
How actionable is the feedback? Snyk delivers developer-friendly, inline remediation guidance, making it easy to fix issues efficiently.
What does Snyk’s Python Code Checker do?
Snyk’s Python Code Checker is an AI-powered static application security testing (SAST) tool designed for Python. It scans your code for both security vulnerabilities and complex bugs (like file I/O corruption, API misuses, null dereferences, threading deadlocks, regex DoS, and more), and provides actionable remediation advice directly within your IDE. It runs scans in real-time and integrates into your existing workflows.
Videos
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)):
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.