Chapter 2. Separation of concerns

 

This chapter covers

  • Using Python’s features for code organization and separation
  • Choosing how and when to separate code into distinct pieces
  • The levels of granularity in separating code

A cornerstone of clear code is the division of its various behaviors into small, manageable pieces. Clear code requires you to keep less knowledge in your head at any given time, making the code simpler to reason about. Short segments of code with clear intent are a big step in this direction, but bits of code should not be broken up along arbitrary boundaries. Separating them by concern is an effective approach.

Definition

A concern is a distinct behavior or piece of knowledge your software deals with. Concerns can range in granularity from how to calculate a square root to how payments are managed in an e-commerce system.

In this chapter, I’ll discuss the tools built into Python for separating the concerns in your code, as well as the philosophy that goes into deciding how and when to use them.

2.1. Namespacing

2.1.1. Namespaces and the import statement

2.1.2. The many masks of importing

2.1.3. Namespaces prevent collisions

2.2. The hierarchy of separation in Python

2.2.1. Functions

2.2.2. Classes

2.2.3. Modules

2.2.4. Packages

Summary

sitemap