Type checking in Python in the right way

Type checking in Python in the right way

Type annotations, type hinting and beyond the `type()` function. This article is about another such function type() and an introduction to built-in types module as well as type hinting in Python.

Introduction

In your first days of learning Python, the guy in the tutorial first introduces you to the ever-existing print("Hello World!") and that’s it! Most people pretty much use the [print](https://docs.python.org/3/library/functions.html#print) function in this fashion for the rest of their coding journey and never look back to explore other features of it. This article is about another such function type() and an introduction to built-in [types](https://docs.python.org/3/library/types.html) module as well as type hinting in Python.

We all know how to check the type of basic built-in objects in Python. However, sometimes, there are cases where you cannot check for the type of an object in a traditional way like this:

variable = 'Python'

    if type(variable) in [set, int, str, dict, list, float, tuple, bool]:
        print('It is a built-in type')
    else:
        print('Maybe not a built-in?')

Let’s say we are dealing with a *generator *object, which is a common alternative to Python lists. How do we check for its type? Surely, doing this won’t work:

generator = (number for number in range(10))

    if type(generator) == "<class 'generator'>":
        print('Are you crazy?')

python data-science programming artificial-intelligence machine-learning

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.