An object is a data structure (state) with a set of functions specific to the object that give a different result depending on the state of the object (its attributes).
Introduction
Object-oriented programming was born with Simula in 1962, and its origin is to simulate objects and interactions between objects.
An object is a data structure (state) with a set of functions specific to the object that give a different result depending on the state of the object (its attributes).
Finally, remember that object-oriented programming is invaluable in creating user interfaces, real object simulation programs (such as 3D design), etc., but not in managing processes that process data such as artificial intelligence, web services, etc.
Object
Data structure
Every programming language is composed of simple data structures that allow you to represent anything, for example, a rectangle:
= 3
= 6But if we have two rectangles, how can we represent them?
= 3
= 6
= 5
= 10It’s not a good solution. What if we use a list?
=
= In this way, we have the properties of a rectangle grouped in a single data structure under a single reference.
Functions
What if we want to calculate the area of a rectangle?

return *
assert == 12Let’s test if it works.
We create a new project:
uv init test
cd test
uv add --dev pytestWe install the pytest dependency in a development environment since we don’t need it in a production environment (–group dev).
We can now run pytest:
uv run pytestBut didn’t we say we were using a list to represent a rectangle?
return *
=
assert == 18We have a function that calculates the area of a rectangle… only if the list it receives as a parameter represents a rectangle.
The truth is that in somewhat complex code many unforeseen things can happen, because you can say that a list of two values is a rectangle, but for Python it’s just a list.
return *
=
assert == 18
= # base, height
assert == 9And it might seem obvious because it’s only a few lines, but after a hundred it’s not fun at all.

Classes
That’s why classes exist in Python:
=
=
return *
# a cube is not a rectangle
=
assert == 18
= # base, height
assert == 9
Python uses duck typing. Therefore, the rectangle_area function accepts any object that has two attributes named length and width.
=
=
=
=
=
return *
# a cube is not a rectangle
=
assert == 18
=
assert == 36Since a cube has the attributes length and width, the rectangle_area function has no problem calculating its area:
When I see a bird that walks like a duck, swims like a duck, and sounds like a duck, I call that bird a duck.
Languages that force you to declare the parameter type do not have this problem.
We can modify the rectangle_area function to verify that the parameter is an instance of the Rectangle class with the isinstance function:
=
=
=
=
=
assert
return *
=
assert == 18
=
assert == 36Now, instead of giving us erroneous data, the function generates a runtime error (in languages like Java the error would be at compile time).
Anyway, if the rectangle_area function should only work with objects of the Rectangle class, why not put it all together?
=
=
return *
=
assert == 3
assert == 18By the way, in UML the Rectangle class is represented like this:
To facilitate the creation of diagrams, you can use our guide to use the Mermaid plugin in VSCode.
Exercises
Below is a UML representation of classes, and you must write the corresponding Python code.
Equilateral triangle
Show solution
=
=
return * Button
Let’s represent a button in a graphical interface:
And its Python coding would be this:
Show solution
=
=
return
return Window
One of the main characteristics of object-oriented programming is object composition.
For example, a window of a graphical interface can have a button:
And its Python coding would be this:
Show solution
=
=
=
=
=
=
= We can create an instance of Window and verify its operation:
=
=
assert ==
= False
=
assert ==
assert == 200
assert == 400Inheritance
TODO