Register to get access to free programming courses with interactive exercises

Creating an abstraction Python: Building data abstractions

The Cartesian coordinate system is not the only way of describing a grid. Another way is the polar coordinate system:

The polar coordinate system is a two-dimensional coordinate system in which we determine each point on the plane by two numbers — the polar angle and the polar radius. The polar coordinate system helps in cases where the relations between points are easier to represent in the form of radii and angles. We can establish such relations only by applying trigonometric equations in more common Cartesian, rectangular, or coordinate systems. (c) Wikipedia

Polar Coordinate System

Imagine this situation. We developed a graphic editor like Photoshop and used a library for working with graphic primitives based on the Cartesian coordinate system. At some point, you realize that switching to the polar system will help make everything easier and faster. What price will you have to pay for this change? We have to rewrite almost all the code:

point = {"x": 2, "y": 3}
symmetrical_point = {
    "x": -point["x"],
    "y": point["y"],
}

It is because your library does not hide the internal structure. Any code that uses points or segments knows how they work inside. It applies to pieces of code that create new primitives and extract the parts from them. It is simple to change the situation and hide the implementation using functions:

point = make_point(3, 4)
symmetrical_point = make_point(-get_x(point), get_y(point))

In the example, we can see three functions:

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.