Reader small image

You're reading from  Python Object-Oriented Programming - Fourth Edition

Product typeBook
Published inJul 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801077262
Edition4th Edition
Languages
Right arrow
Authors (2):
Steven F. Lott
Steven F. Lott
author image
Steven F. Lott

Steven Lott has been programming since computers were large, expensive, and rare. Working for decades in high tech has given him exposure to a lot of ideas and techniques, some bad, but most are helpful to others. Since the 1990s, Steven has been engaged with Python, crafting an array of indispensable tools and applications. His profound expertise has led him to contribute significantly to Packt Publishing, penning notable titles like "Mastering Object-Oriented," "The Modern Python Cookbook," and "Functional Python Programming." A self-proclaimed technomad, Steven's unconventional lifestyle sees him residing on a boat, often anchored along the vibrant east coast of the US. He tries to live by the words “Don't come home until you have a story.”
Read more about Steven F. Lott

Dusty Phillips
Dusty Phillips
author image
Dusty Phillips

Dusty Phillips is a Canadian software developer and an author currently living in New Brunswick. He has been active in the open-source community for 2 decades and has been programming in Python for nearly as long. He holds a master's degree in computer science and has worked for Facebook, the United Nations, and several startups.
Read more about Dusty Phillips

View More author details
Right arrow

The Intersection of Object-Oriented and Functional Programming

There are many aspects of Python that appear more reminiscent of structural or functional programming than object-oriented programming. Although object-oriented programming has been the most visible paradigm of the past two decades, the old models have seen a recent resurgence. As with Python's data structures, most of these tools are syntactic sugar over an underlying object-oriented implementation; we can think of them as a further abstraction layer built on top of the (already abstracted) object-oriented paradigm. In this chapter, we'll be covering a grab bag of Python features that are not strictly object-oriented:

  • Built-in functions that take care of common tasks in one call
  • An alternative to method overloading
  • Functions as objects
  • File I/O and context managers

The case study in this chapter will revisit some of the essential algorithms of k-nearest neighbor...

Python built-in functions

There are numerous functions in Python that perform a task or calculate a result on certain types of objects without being methods on the underlying class. They usually abstract common calculations that apply to multiple types of classes. This is duck typing at its best; these functions accept objects that have certain attributes or methods, and are able to perform generic operations using those methods. We've used many of the built-in functions already, but let's quickly go through the important ones and pick up a few neat tricks along the way.

The len() function

One simple example of functions that are related to object methods is the len() function, which returns the number of items in some kind of container object, such as a dictionary or list. You've seen it before, demonstrated as follows:

>>> len([1, 2, 3, 4])
4

You may wonder why these objects don't have a length property instead of having to...

An alternative to method overloading

One prominent feature of many object-oriented programming languages is a tool called method overloading. Method overloading refers to having multiple methods with the same name that accept different sets of parameters. In statically typed languages, this is useful if we want to have a method that accepts either an integer or a string, for example. In non-object-oriented languages, we might need two functions, called add_s and add_i, to accommodate such situations. In statically typed object-oriented languages, we'd need two methods, both called add, one that accepts strings, and one that accepts integers.

In Python, we've already seen that we only need one method, which accepts any type of object. It may have to do some testing on the object type (for example, if it is a string, convert it to an integer), but only one method is required.

The type hints for a parameter that can take on multiple types...

Functions are objects, too

There are numerous situations where we'd like to pass around a small object that is simply called to perform an action. In essence, we'd like an object that is a callable function. This is most frequently done in event-driven programming, such as graphical toolkits or asynchronous servers; we'll see some design patterns that use it in Chapter 11, Common Design Patterns, and Chapter 12, Advanced Design Patterns.

In Python, we don't need to wrap such methods in a class definition because functions are already objects! We can set attributes on functions (though this isn't a common activity), and we can pass them around to be called at a later date. They even have a few special properties that can be accessed directly.

Here's yet another contrived example, sometimes used as an interview question:

>>> def fizz(x: int) -> bool:
...     return x % 3 == 0
>>> def buzz(x: int) ->...

File I/O

Our examples so far that have touched the filesystem have operated entirely on text files without much thought as to what is going on under the hood. Operating systems represent files as a sequence of bytes, not text. We'll take a deep dive into the relationship between bytes and text in Chapter 9Strings, Serialization, and File Paths. For now, be aware that reading textual data from a file is a fairly involved process, but Python takes care of most of the work for us behind the scenes.

The concept of files has been around since long before anyone coined the term object-oriented programming. However, Python has wrapped the interface that operating systems provide in a sweet abstraction that allows us to work with file (or file-like, vis-à-vis duck typing) objects.

The confusion arises because the operating system file and the Python file object are both, commonly, called "files." It's difficult to be ultra-cautious and...

Case study

While object-oriented programming is helpful for encapsulating features, it's not the only way to create flexible, expressive, and succinct application programs. Functional programming emphasizes functional design and function composition over object-oriented design.

In Python, functional design often involves using a few object-oriented techniques. This is one of the beauties of Python: being able to choose an appropriate set of design tools to address the problem effectively.

We often depict object-oriented designs with the classes and their various associations. For functional design, we're interested in functions to transform objects. A functional design can follow mathematical practices closely.

In this part of the case study, we'll revisit a number of features of the classifier as functions mixed with class definitions. We'll step away from a pure object-oriented view and adopt a hybrid view. In particular, we'll look closely at...

Recall

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 overloading" – 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...

Exercises

If you haven't encountered with statements and context managers before, I encourage you, as usual, to go through your old code, find all the places where you were opening files, and make sure they are safely closed using the with statement. Look for places to write your own context managers as well. Ugly or repetitive try...finally clauses are a good place to start, but you may find them useful any time you need to do before and/or after tasks in context.

You've probably used many of the basic built-in functions before now. We covered several of them, but didn't go into a great deal of detail. Play with enumeratezipreversedany, and all until you know you'll remember to use them when they are the right tool for the job. The enumerate function is especially important because not using it results in some pretty ugly while loops.

Also explore some applications that pass functions...

Summary

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.

In the next chapter, we'll learn more about string and file manipulation, and even...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python Object-Oriented Programming - Fourth Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781801077262
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Steven F. Lott

Steven Lott has been programming since computers were large, expensive, and rare. Working for decades in high tech has given him exposure to a lot of ideas and techniques, some bad, but most are helpful to others. Since the 1990s, Steven has been engaged with Python, crafting an array of indispensable tools and applications. His profound expertise has led him to contribute significantly to Packt Publishing, penning notable titles like "Mastering Object-Oriented," "The Modern Python Cookbook," and "Functional Python Programming." A self-proclaimed technomad, Steven's unconventional lifestyle sees him residing on a boat, often anchored along the vibrant east coast of the US. He tries to live by the words “Don't come home until you have a story.”
Read more about Steven F. Lott

author image
Dusty Phillips

Dusty Phillips is a Canadian software developer and an author currently living in New Brunswick. He has been active in the open-source community for 2 decades and has been programming in Python for nearly as long. He holds a master's degree in computer science and has worked for Facebook, the United Nations, and several startups.
Read more about Dusty Phillips