Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Callables

00:00 In the previous lesson, you saw how to get more information about classes and objects. In this lesson, you’ll learn more about callables. I know I’ve been beating this drum a bit, but everything in Python is an object.

00:13 See, I behaved myself that time. That means both functions and methods on those functions are also objects. You saw a bit of this when I associated a function with a class using the built-in type() function as a class creator in the previous lesson.

00:28 Since functions and methods are objects, you can reference them as data. You do this by using them without the parentheses. The parentheses are what actually invokes the function or method, calling it.

00:40 A callable is any object in Python that can be invoked this way, and the list is longer than you might expect. Of course, you’ve seen it with functions and methods, but you can also make an object instance callable through the definition of the .__call__() method.

00:55 Python also has function closures and generator functions, so lots of different ways of invoking a named chunk of code. The callable() built-in tells you whether or not an object is callable.

01:08 Let’s go look at some examples. To see the difference between a function and a reference to the function, let me start with the old standard. No surprise there if you’re used to using print(), but stop and think about the phrase that begins with everything in Python.

01:27 If you evaluate print on the REPL without the parentheses, you’re not invoking it. You see that it is a built-in function. The parentheses invoke it. Without those, you get a reference.

01:39 There have been little hints to this throughout the course. Remember the built-in iter() function? It can take a reference to a callable. In the lesson on iter(), I passed in a reference to the input() function.

01:50 Not calling it, but passing in the reference. Then when the iterable got iterated, the iterator invoked input() each time. Let’s go back to print().

02:01 The callable() function takes a single argument, the thing to check for callability. I’m just making words up now. It’s fun. print, of course, is callable, so callable() returns True when it’s passed in as an argument.

02:14 Let’s look at some other things.

02:18 This should be familiar from the data types section. Calling int() with a float argument returns an integer instance. At the time, I referred to this as a built-in function, but that was a little bit of a lie.

02:31 It isn’t really a function. Evaluating int on its own tells you that it’s a class. Although it doesn’t follow the convention of starting with an uppercase letter, when you invoke int() with a float, what you’re really doing is instantiating the int class, giving you an integer object.

02:50 And since you’re invoking it using the parentheses protocol, int must be callable.

02:57 To be clear, it’s the class that’s callable, not the resulting object in this case. That’s not to say that objects can’t be. I’ll come back to that in a minute.

03:07 This leads to an interesting little thing. Although the built-in functions are referred to as, well, built-in functions, they technically are built-in callables.

03:16 Nobody calls them that, but if you dig, you’ll find many of the things you’ve seen so far aren’t actually functions. abs tells you that it’s a built-in function.

03:26 Same goes for bin. But bool, like int, is a class. In fact, most of the data type ones are actually classes. Although I explained these as functions that were doing conversion, they aren’t really. They are instantiators creating new instances of those classes.

03:43 They work like conversion tools because the constructors to the classes allow different types as arguments and then convert them to the instance type. chr is actually a function,

03:57 while dict, a data type, is a class. But the pattern isn’t always obvious.

04:03 enumerate is a class, as is range.

04:09 hasattr is a function, while the property decorator is itself a class. That’s a little mind-boggling. You use it like a decorator, so it’s a built-in function that modifies the methods, but really it’s a class.

04:25 If that one hurts your brain, well, then the next one’s going to induce migraines. The built-in type() function is a class, a class called type.

04:36 Using type() on some of our built-ins gives similar information to evaluating the references.

04:41 For example, the type of abs is the built-in function or method class. Now for the weird one.

04:50 The type of the built-in data types like bool is the type class, just like the built-in function type itself. When you evaluate type, you get type.

05:02 This relates back to that metaclass stuff I mentioned in the earlier lesson. Everything might be an object, but it isn’t turtles all the way down. Something has to be at the bottom.

05:11 The type type is the magic behind it all, and along with metaclasses are how the underlying object and class system work. As I mentioned before, this here be black magic, and most programmers never have to think about it at all.

05:25 But it’s there if you’re curious. One more quick example before moving on. Let me show you how you can make an object instance callable. I’ll create a quick stub class first. Now I’ll instantiate it.

05:44 The Person class is callable because that’s how you create instances.

05:50 But the resulting instance is not. You can change that with a little extra work.

05:57 Defining a class,

06:00 and here’s the .__call__() method. This is what makes your object instance callable. Since it’s a method, it needs self as an argument, although you can add more arguments than that if you want to.

06:12 If you do, those extra arguments are the ones you use when you invoke the object as a callable.

06:20 I’m keeping it simple, just trying to prove that this works. And now I’ll get an instance.

06:29 Of course, Car is callable.

06:33 But now the instance is as well. And like any callable, you invoke it with parentheses. Vroom vroom. Besides being a neat trick, this is useful when you want to create function-like things that need to track state.

06:47 Like any other object, callable ones can have data associated with them, meaning you can get the interface of a function with the state storage of an object.

06:58 I already mentioned this video course and tutorial in the previous lesson, but I’m including it here in case you’re hopping around and not doing the lessons in order. If you want to learn more about the black magic of type types and metaclasses, this is where to start.

07:12 Beware, it’s an advanced topic. The next lesson is the last in this section, and it covers built-in functions having to do with class hierarchies and the granddaddy of them all, the object object.

Become a Member to join the conversation.