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!
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__
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?
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!
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__
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
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…
To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.
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.
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...
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...