Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __call__() Magic Method

#1
Python __call__() Magic Method

Syntax


object.__call__(self[, args...])

The Python __call__ method makes a class callable, so you can call objects of the class like a normal function. For example, if you define __call__(self) on an object x of class X, you can call it like so: x(). The return value of the called object is the return value of the __call__() method.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

đź’ˇ Note: Formally, calling x(arg1, arg2, ...) translates to type(x).__call__(x, arg1, ...) whereas the type() built-in function determines the class (type) of x.

Minimal Example __call__


The following code snippet makes a custom class Person callable by providing an implementation of the magic method __call__() that takes two arguments:

  • self – a reference to the object itself. This is automatically passed into the function by Python—you don’t need to provide the argument yourself.
  • other – an additional argument (e.g., a string) to provide a custom greeting.

With this implementation, we can now create a callable object alice and call it like so: alice().

class Person: def __call__(self, other): return f'Hi {other}' alice = Person()
print(alice('Bob'))
# Hi Bob

Background callable





Python’s built-in callable(object) returns True if you could call the object argument like a function with the trailing parentheses in object().

You can make any object callable by implementing the instance’s __call__() method.

For example, callable(callable) returns True because callable is a function object. But callable(3) returns False because an integer is not a function you can call.

>>> callable(callable)
True
>>> callable(3)
False

You can learn more about the function in our detailed blog guide:

References:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2021/12/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
News - Video: We Have A Lot Of Feelings ...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 1
News - Iconic Rockstar And Actor Meat Lo...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 0
(Indie Deal) FREE Last Horizon, Microids...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 9
Microsoft - Celebrating 20 years of Trus...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
Xbox Wire - New EA Play Member Rewards F...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 2
News - PS5 Will Outsell Series X|S 2:1 I...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
[Tut] Pandas get_dummies() – A Simple Gu...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 19
Mobile - Genshin Impact’s Tomo – vision,...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 5
News - Community Focus: Parris
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 7
News - Bedrock: BETA 1.18.10.28
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 5

[-]
Twitter



Discord Server © SickGaming.net 2012-2021