__call__ in Python

preview_player
Показать описание
In this video, we will discuss the __call__ method and how to implement it in a class in Python.

When we invoke a function, we simply use the function name with parentheses(func()). This notifies the interpreter that a function is being called. Internally, it is equivalent to this expression func.__call__(), because functions in Python are objects and the call method is what gets executed when the function is invoked.

So, when we call a function in Python, the interpreter executes the __call__ method of the function object behind the scenes.

Chapters:
⏩ 0:00 Intro to __call__
⏩ 0:44 Basic example
⏩ 0:57 Where to use __call__ with example
⏩ 1:27 Real-life example

✨More on 2MinutesPy👇👇

@2MinutesPy

#class #method #objectorientedprogramming #oop #python
Рекомендации по теме
Комментарии
Автор

Never seen this before. First half of the video I was like 'ok, another dunder method, so what', then the example with the API makes all the sense in the world... Thanks!

sebastianmorales
Автор

Never ever a day goes when I feel how underrated this channel is, Pls start a series on something around this ❤

spidey
Автор

I have been looking for a Python daily refresher, and thankfully, I found your channel. Thank you for the high-quality, short, and informative videos.

youngsci
Автор

I really want this video to go viral, it's just great 👍. Thank you 2minutespy

dweepverma
Автор

It's a great channel. I routinely struggle when I'm studying programming, because I can't grasp the content well when I don't get the background of it. Knowing how a bit of it works is, as someone in the comments already said, refreshing! There is some specific name for those aspects and processes that happen behind the scenes in Python? I would like to search it more profoundly by myself

(Sorry for any English misspelling, learning yet)

pedrohfahrenheit
Автор

Nice one! Short clear and useful learning video = Subscribed!
Just to confirm my understanding- you could have made a function inside the class (eg callAPI) and call it like this: response = client.callAPI("users", id=123), but this dunder call is the easy to use/shortcut version of the same, by having a default function (__call__) be called directly? No other real change I presume?

DavidZammit
Автор

So the dunder call can be used as a constructor for a class too?

ombiradar
Автор

The example is not much helpful. Because i can achieve the same functionality by calling a method.
Any specific example where u don't have any choice but to use __call__ method

futanyy