Properties and encapsulation
In Python attributes are public by convention: there is no private . A leading underscore ( _balance ) warns that something is…
As programs grow, classes need contracts and structure. In this lesson you will see properties with validation, abstract classes, special methods so your objects behave like containers and functions, and the most useful design patterns in Python: Strategy, Factory, Observer, State and Command. You will learn to share responsibilities and to add behavior without rewriting what already works.
In Python attributes are public by convention: there is no private . A leading underscore ( _balance ) warns that something is…
An abstract class defines *what* its subclasses must be able to do without saying *how*. It is created by inheriting from abc.ABC…
A design pattern is a known solution to a recurring problem. In Python many of them are much shorter than in other languages…
An object publishes events and others subscribe to react, without the emitter knowing who they are. It is the basis of the event…
Special methods (those that start and end with double underscores) let your classes integrate with the language syntax: len(x) …
A property ( @property ) looks like an attribute but runs code when read or assigned: it is used to validate and to expose…
Derived data (area, perimeter…) is not stored: it is computed when requested through a read-only property, so it never gets out…
An abstract class ( abc.ABC with @abstractmethod ) defines a contract: it forces subclasses to implement certain methods and…
The Strategy pattern separates “which algorithm I use” from “where I use it”: in Python a dictionary that maps a name to a…
A factory creates objects from a name. With a registry (a dictionary) and a decorator, each class registers itself, and the…
In the Observer pattern some objects ( subscribers ) register to be notified when another one (the emitter ) publishes an event…
With __call__ an object can be called as if it were a function, and at the same time store data. It is useful for functions with…
The special methods __getitem__ , __setitem__ , __len__ , __contains__ and __iter__ make your class behave like a native…
The State pattern represents each state as a class that knows which other states can be reached. That way the valid transitions…
In the Command pattern each action is an object with a method to execute it and another to undo it. By storing the executed…