Object-Oriented Programming in Python
Object-oriented programming (OOP) 1 is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).
Note
A class is a kind of "blueprint", according to which objects are built — instances of this class.
UML
Classes, attributes, methods
classDiagram
class ClassName{
+public_attribute
-private_attribute
...
+public_method()
-private_method()
}
Attributes or fields — state. Methods — behavior. ...
- means that the real class contains other elements, but
they are not essential.
Abstract classes and methods
classDiagram
class AbstractClassName{
<<abstract>>
...
+abstract_method()*
}
Relationships between objects
mermaid
documentation: Defining Relationship.
classDiagram
classA --|> classB : Inheritance
classC --* classD : Composition
classE --o classF : Aggregation
classG --> classH : Association
classI -- classJ : Link(Solid)
classK ..> classL : Dependency
classM ..|> classN : Realization
classO .. classP : Link(Dashed)
- Dependency: Class A may be affected by changes to class B.
- Association: Object A knows about object B. Class A depends on B.
- Aggregation: Object A knows about object B and consists of it. Class A depends on B.
- Composition: Object A knows about object B, consists of it and manages its life cycle. Class A depends on B.
- Realization: Class A defines methods declared by interface B. Objects of A can be viewed through interface B. Class A depends on B.
- Inheritance: Class A inherits the interface and implementation of class B, but can override it. Objects A can viewed through the interface of class B. Class A depends on B.
Inheritance
Inheritance is the ability to create new classes based on existing ones. The main benefit of inheritance is reusing existing code. The payoff for inheritance is that subclasses always follow interface of the parent class. You cannot exclude from a subclass a method declared in its parent.
Inheritance models what is called an is a relationship. This means that when you have a Derived
class that
inherits from a Base
class, you created a relationship where Derived
is a specialized version of Base
.
Let’s say you have a base class Animal
and you derive from it to create a Horse
class. The inheritance
relationship states that a Horse
is an Animal. This means that Horse
inherits the interface and
implementation of Animal
, and Horse
objects can be used to replace Animal
objects in the application.
This is known as the Liskov substitution principle.
Liskov substitution principle
In a computer program, if S
is a subtype of T
, then objects of type T
may be
replaced with objects of type S
without altering any of the desired properties of
the program.
Note
The biggest problem is not so much the number of classes in your design, but how tightly coupled the relationships between those classes are. Tightly coupled classes affect each other when changes are introduced.
Composition
Composition is a concept that models a has a relationship. It enables creating complex types by combining
objects of other types. This means that a class Composite
can contain an object of another class Component
. This
relationship means that a Composite
has a Component
.
For example, your Horse
class can be composed by another object of type Tail
. Composition allows you to express
that relationship by saying a Horse
has a Tail
.
Composition enables you to reuse code by adding objects to other objects, as opposed to inheriting the interface and
implementation of other classes. Both Horse
and Dog
classes can leverage the functionality of Tail
through
composition without deriving one class from the other.
Abstraction
Program objects only model the properties and behaviors of real objects that are important in a particular context, and the rest are ignored.
Abstraction is a model of some object or phenomenon of the real world, in which minor details are omitted, not playing an important role in this context.
Encapsulation
Interface — public part of the object, available to other objects.
Encapsulation is the ability of objects to hide some of their state and behavior from other objects by providing to the outside world only a certain interface of interaction with oneself.
Polymorphism
Polymorphism is the ability of a program to choose different implementations when calling operations with the same name.
What is object
?
Read about it in the New-style and classic classes section of the Python 2 documentation.
Exceptions
All exceptions must derive from BaseException
: BaseException
→ Exception
→ MyCustomError
.