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 less knowledge to be kept 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 some arbitrary boundary; separating them by concern is an effective approach.


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 of your code as well as the philosophy that goes into deciding how and when to use them.


If you haven’t yet, you’ll want to set up Python on your computer to follow along with the code in this book. The installation and best practices are all covered in appendix A, so before you go too much further you should head there and get situated! I’ll be right here when you’re ready.

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

2.3  Summary



Unfortunatly, we weren't able to load your books.

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes) homepage