Summarize the concept explored in this chapter regarding object-oriented and functional programming intersection in Python.

We'll cover the following


We’ve touched on a number of ways that object-oriented and functional programming techniques are part of Python:

  • Python built-in functions provide access to special methods that can be implemented by a wide variety of classes. Almost all classes, most of them utterly unrelated, provide an implementation for __str__( ) and __repr__() methods, which can be used by the built-in str() and repr() functions. There are many functions like this where a function is provided to access implementations that cut across class boundaries.
  • Some object-oriented languages rely on _method overloadin_g – a single name can have multiple implementations with different combinations of parameters. Python provides an alternative, where one method name can have optional, mandatory, position-only, and keyword-only parameters. This provides tremendous flexibility.
  • Functions are objects and can be used in ways that other objects are used. We can provide them as argument values; we can return them from functions. A function has attributes, also.
  • File I/O leads us to look closely at how we interact with external objects. Files are always composed of bytes. Python will convert the bytes to text for us. The most common encoding, UTF-8, is the default, but we can specify other encodings.
  • Context managers are a way to be sure that the operating system entanglements are correctly cleaned up even when there’s an exception raised. The use goes beyond simply handling files and network connections, however. Anywhere we have a clear context where we want consistent processing on entry or exit, we have a place where a context manager can be useful.


We covered a grab bag of topics in this chapter. Each represented an important non- object-oriented feature that is popular in Python. Just because we can use object- oriented principles does not always mean we should.

However, we also saw that Python typically implements such features by providing a syntax shortcut to traditional object-oriented syntax. Knowing the object-oriented principles underlying these tools allows us to use them more effectively in our own classes.

We discussed a series of built-in functions and file I/O operations. There are a whole bunch of different syntaxes available to us when calling functions with arguments, keyword arguments, and variable argument lists. Context managers are useful for the common pattern of sandwiching a piece of code between two method calls. Even functions are objects, and, conversely, any normal object can be made callable.

Get hands-on with 1200+ tech skills courses.