Skip to content
All apps

Swap the inside

About this app

A training log with an interface (eintragen, gesamtstrecke) and an X-ray view of its inside: three versions store the runs in completely different ways. The program that only uses the methods works in all three versions; the program that reaches into the inside breaks with the first rebuild. The underscore does not protect either; it is a request.

Why encapsulation matters

Encapsulation means that an object offers an interface of methods and hides how it stores its data internally. Code that uses only the interface notices nothing when the inside is rebuilt. Code that accesses internal attributes directly is tied to one particular design and breaks with the first change. The idea goes back to David Parnas. In Python a leading underscore, as in _strecke, protects nothing technically; it is a convention that says: please do not use this from outside.

What the app shows

In the middle is the object buch as a casing with the buttons eintragen and gesamtstrecke and an X-ray window into its inside. On the left runs a program that uses only the interface; on the right one that accesses laeufe, distanz and _strecke directly. Three versions A, B and C store the runs completely differently: as a list of runs, only as sums, or only as a list of distances. Every line shows whether it runs, fails, or runs but reaches inside.

Try it yourself

Switch between versions A, B and C and watch the inside change in the X-ray view. On the left the result is 17.5 km in all three versions. On the right, results and AttributeErrors change from version to version. Look closely at buch._strecke in version B: the line runs, but it still breaks the contract.

Frequently asked questions

What does encapsulation mean in object-oriented programming?
Encapsulation separates an object's interface, the methods through which it is used, from its internal representation. Other parts of the program rely only on the interface. The inside can therefore change without any code outside having to be adapted.
Does an underscore protect an attribute in Python?
No. A single underscore such as _strecke is just a convention signalling that the attribute is internal. Python does not prevent access. Two underscores trigger name mangling, which changes the name, but that is not real access protection either.
What belongs to a class's interface?
Everything users of the class may rely on: public methods with their parameters, return values and documented behaviour. Internal attributes and helper methods are not part of it and may change at any time.
What does the app Swapping the inside show?
A training log in three versions with the same interface. A program that uses the methods runs in every version; a program with direct access to the inside breaks as soon as you switch versions. An X-ray view shows each internal design.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps