liveBook · Manning

Coupling

As explained in Python How-To

Loose coupling is a software design condition in which components have minimal dependencies on one another's implementation details.

Loose coupling keeps related classes useful together without making them depend heavily on one another’s implementation details. This separation improves maintainability because changing one class is less likely to require changes elsewhere. section 9.5.3 section 9.5.4

Use these design practices when connecting related classes:

  • Give each class a distinct concern, such as storing account data separately from demographic data. section 9.5.3
  • Let callers use the specialized class directly instead of adding forwarding methods to another class. section 9.5.3
  • Review class relationships throughout development; use flatter models when inheritance creates dependencies that make updates difficult. section 8.5.5 section 9.5.4
Listing 9.10 Separating classes to have distinct concerns
class Account:
   def __init__(self, student_id):
       # query the database to get additional information using student_id
       self.account_number = 123456
       self.balance = 100

class Demographics:
   def __init__(self, student_id):
       # query the database to get additional information using student_id
       self.age = 12
       self.gender = "Female"
       self.race = "Black"
      
class Student:
   def __init__(self, first_name, last_name, student_id):
       self.first_name = first_name
       self.last_name = last_name
       self.student_id = student_id
       self.account = Account(self.student_id)
       self.demographics = Demographics(self.student_id)

Inspect a Python design that keeps Account, Demographics, and Student as separate classes while composing the specialized objects inside Student. section 9.5.3

Student data model

A Student object can contain Account and Demographics objects, while callers read account balance or demographic attributes from those specialized objects. Adding database retrieval methods inside Account or Demographics then avoids placing their implementation details in Student. section 9.5.3

How Python How-To covers it

How deep it goes
Explains the design rationale, contrasts forwarding methods and specialized classes, and stops short of formal coupling metrics.
Examples
Uses Student, Account, and Demographics classes, including direct attribute access and an alternative forwarding-method design.
What it assumes
Assumes familiarity with Python classes and objects while teaching how class relationships affect maintainability from concrete examples.
What you can do after
Build it from scratch by separating class concerns and avoiding methods that unnecessarily expose another class’s implementation.
How it connects
Connects loose coupling with classes, inheritance, data models, distinct responsibilities, UML diagrams, and refactoring.

Also explained in

Compare how 17 books explain coupling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.