Understanding Callable in Python

Understanding Callable in Python

Functions and classes are the most common things we use in our daily development. We invoke them, pass them around, and yet never wonder what makes them so amazing. Well the short answer is callable protocol; for the long answer keep reading!

Functions and classes are the most common things we use in our daily development. We invoke them, pass them around, and yet never wonder what makes them so amazing. Well the short answer is callable protocol; for the long answer keep reading!

Definition 📢

Objects defining the __call__ method is known as callable. Or basically callable is anything you can call using the parenthesis () and pass arguments to it. Yes I am basically talking about a function.

call method 🤙

__call__ is one of the most interesting dunder method in Python. It is what most of the built-in functions make use of. If we peek into the type of some of these built-in functions then we often see the result as a class.

>>> range
<class 'range'>

>>> zip 
<class 'zip'>
>>> int
<class 'int'>

You get the gist. But how is a class acting as a function? I mean you can just zip(iterable1, iterable2)and you get your result without further invoking any other methods of the class.

Just imagine using **`zip` like if it were a normal class.**

processor = ['Intel', 'Ryzen', 'Apple Silicon']
year = [2018, 2019, 2020]

zipped = zip(processor, year)
zipped.start()

This definitely isn’t intuitive to work with. So how does it work?

Using call method 🔨

Well behind the scenes it’s because of the __call__ method. This method is used in classes if we want the instance of the class to be callable.

What do I mean by that? Let’s take an example of a class that prints the square of a number.Functions and classes are the most common things we use in our daily development. We invoke them, pass them around, and yet never wonder what makes them so amazing. Well the short answer is callable protocol; for the long answer keep reading!

Definition 📢

Objects defining the __call__ method is known as callable. Or basically callable is anything you can call using the parenthesis () and pass arguments to it. Yes I am basically talking about a function.

call method 🤙

__call__ is one of the most interesting dunder method in Python. It is what most of the built-in functions make use of. If we peek into the type of some of these built-in functions then we often see the result as a class.

>>> range
<class 'range'>

>>> zip 
<class 'zip'>
>>> int
<class 'int'>

You get the gist. But how is a class acting as a function? I mean you can just zip(iterable1, iterable2)and you get your result without further invoking any other methods of the class.

Just imagine using **`zip` like if it were a normal class.**

processor = ['Intel', 'Ryzen', 'Apple Silicon']
year = [2018, 2019, 2020]

zipped = zip(processor, year)
zipped.start()

This definitely isn’t intuitive to work with. So how does it work?

software-engineering software-development python programming coding

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

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Developer or Engineer? Does It Make a Difference?

To summarise the main differences between the software developer and engineer: A developer executes. ... So the software developer is mainly focused on developing code that is a part of software development cycle. An engineer designs and plans applying the principles of engineering to software development.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...