
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What's the canonical way to check for type in python?
If you want to check if an object, x is an instance of exactly a given type(not a subtype), you can use typeto get its type and check using is statement.
example
x = "Hello" if type(x) is str: print("x is an instance of str")
Output
This will give the output
x is an instance of str
If you want to check if x is an instance of a MyClass or any subclass of MyClass, you can use the isinstance method call.
example
x = "Hello" if isinstance(x, str): print("x is an instance of str")
Output
This will give the output
x is an instance of str
- Related Questions & Answers
- What's the simplest way to print a Java array?
- What's the best way to do this? VBA, etc
- What's the best way to ensure a happy marriage?
- What's the best way to trim std::string in C++?
- What's the best way to detect a 'touch screen' device using JavaScript?
- What's the best way to open new browser window using JavaScript?
- How to detect the browser's format for HTML input type date
- What's the best place for python classes in a Django project?
- What's the fastest way of checking if a point is inside a polygon in Python?
- What is the canonical label?
- The simplest way to get the user's current location on Android
- What's the Kotlin equivalent of Java's String[]?
- Easy way to remember Strassen's Matrix Equation in C++
- What is the associativity of Python's ** operator?
- Where's the standard python exception list for programmers to raise?
Advertisements