Skip to Content
Mastering Object-oriented Python
book

Mastering Object-oriented Python

by Steven F. Lott
April 2014
Beginner to intermediate
634 pages
15h 22m
English
Packt Publishing
Content preview from Mastering Object-oriented Python

Callables

Python's definition of callable object includes the obvious function definitions created with the def statement.

It also includes, informally, any class with a __call__() method. We can see several examples of this in Python 3 Object Oriented Programming, Dusty Phillips, Packt Publishing. For it to be more formal, we should make every callable class definition a proper subclass of collections.abc.Callable.

When we look at any Python function, we see the following behavior:

>>> abs(3)
3
>>> isinstance(abs, collections.abc.Callable)
True

The built-in abs() function is a proper instance of collections.abc.Callable. This is also true for the functions we define. The following is an example:

>>> def test(n): ... return n*n ... >>> isinstance(test, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781783280971Supplemental Content