liveBook · Manning

Black box

As explained in Get Programming: Learn to code with Python

A black box is an abstraction of a system whose internal steps are hidden while its inputs, outputs, and overall behavior remain visible.

A black box is a way to understand a task without first seeing every step inside it. Start with what goes in, what the task does, and what comes out. The hidden steps are its implementation: the way you write the code to complete the task. section 20.1 section 2.3

To sketch a black box before coding:

  • Write down sample inputs and the outputs you expect. section 2.3
  • Ask whether the task interacts with a user or behaves differently in special situations. section 2.3
  • For a larger task, draw smaller boxes and document what each one expects and returns. section 20.1

Changing a TV channel

Pressing a remote button produces a signal. The TV receives that signal, and what you see or hear changes. You can describe both devices by their inputs, behavior, and outputs without knowing how to assemble either one. section 20.2

In a program, a small black box can become a code module: a piece of code for one task. Modules can pass outputs to one another to accomplish a larger goal. Keeping their details out of view helps you focus on how the pieces fit together; writing each task separately also makes it easier to test. section 20.2 section 20.4

Figure 20.2. “Find in warehouse” shown with and without a black box over the task. Seeing the details of how the item is found and retrieved in the warehouse doesn’t add any more understanding of the task itself.

Compare the warehouse task with and without its hidden steps. The inputs and outputs stay the same, even when the retrieval details are out of view. section 20.1

Quick check: If a program finds an item in a warehouse, what must another part of the program provide, and what should it receive back? Answer that before deciding how the search works. section 20.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines the abstraction and gives steps for visualizing tasks and documenting module boundaries, while leaving implementation details hidden.
Examples
Uses bread-making, warehouse retrieval, and a TV remote to make inputs, behavior, and outputs concrete.
What it assumes
Assumes familiarity with programs that request input, perform operations, and show output; introduces code modules as the next step.
What you can do after
Practice/perform it by diagramming a task’s inputs and outputs, then dividing a larger program into testable, reusable modules.
How it connects
Connects to abstraction, implementation, modularity, code modules, inputs, outputs, and testing.

Also explained in

Compare how 6 books explain black box →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.