Loading video player…

Create Callable Instances With Python's .__call__() (Overview)

In Python, a callable is any object that you can call using a pair of parentheses and, optionally, a series of arguments. Functions, classes, and methods are all common examples of callables in Python. Besides these, you can also create custom classes that produce callable instances. To do this, you can add the .__call__() special method to your class.

Instances of a class with a .__call__() method behave like functions, providing a flexible and handy way to add functionality to your objects. Understanding how to create and use callable instances is a valuable skill for you as a Python developer.

In this video course, you’ll:

  • Understand the concept of callable objects in Python
  • Create callable instances by providing your classes with a .__call__() method
  • Understand the difference between .__init__() and .__call__()
  • Code several examples of using callable instances to solve real-world problems

Resource mentioned in this lesson: Object-Oriented Programming (OOP) in Python

Download

Course Slides (.pdf)

3.0 MB
Download

Sample Code (.zip)

3.1 KB

00:00 Hello, and welcome to Create Callable Instances With Python’s .__call__() method. My name’s Joseph, and you might say that being your instructor for this video course was my calling.

00:10 If you’ve ever used functions in Python, and I’m pretty sure you have, you know that they need to be called with a pair of parentheses. And whatever you put in those parentheses gets passed to the function.

00:20 You might even know this applies to other kinds of objects, like class constructors, but did you also know that you can create your own classes whose instances have the same calling behavior?

00:30 Specifically, you can use .__call__() to create classes with callable instances that can then be used like functions. Learning how to work with these callable instances in Python will open up a whole new world of options for designing classes and interfaces.

00:43 And along the way, you’ll deepen your knowledge about Python internals, as well as object-oriented programming. But first, a quick note on terminology: dunder, no, it’s not slang for Australia.

00:55 That’s just how we refer to these special double-underscore methods in Python. Dunder is a lot easier to say. So instead of .__foo__(), wrapped in double underscores, you would say “dunder foo”.

01:06 Okay, cool. Now, where was I? In this course, you’ll learn the way Python handles callable objects, the role of special methods in Python’s data model, how to create your own classes with callable instances, the differences between the .__init__() and .__call__() methods, as well as techniques for using callable instances to solve real-world problems.

01:28 We do expect you’re familiar with some intermediate Python concepts, like functions and methods, classes and instances, and the general principles of object-oriented programming in Python.

01:41 So if you’re feeling like a review of OOP, check out Object-Oriented Programming in Python.

01:47 Alright, next up, let’s dive deep into the concept of callables in Python.

Become a Member to join the conversation.