Register to get access to free programming courses with interactive exercises

Interfaces Python: Building data abstractions

The term interface is widespread in IT and everyday life. For example, a user interface can be a set of controls for a Web site, an ATM, or a telephone. The interface of a TV remote control is the buttons. The interface in a car would be the levers, buttons, and steering wheel. In summary, the interface determines how you interact with a given system.

Designing interfaces isn't as easy as it might seem. We encounter inconvenient interfaces — when we open doors or use elevators. The more complex the system, the more possible states, the more difficult it is to make the interface. Even in a primitive example of a TV power button with two states — on/off.

You can implement one or two buttons that behave differently depending on the current state.

Programming is similar. An interface is a set of functions with names and signatures that specify the number and types of incoming parameters and the return value, which doesn't depend on any particular implementation. This definition corresponds to the concept of abstract data types. For example, for points, all the functions we have implemented in practice and described in theory are interface functions.

How are the concepts of abstraction and interface related? Abstraction is a word that primarily describes the data we work with. For example, almost every web application contains a user abstraction. Hexlet has the abstractions course, project, and others. An interface is a set of functions helping to interact with data.

But there aren't just interface functions, but also helper functions that are not designed for calling code and are used exclusively within the abstraction:

# The functions `make_user`, `get_age`, `is_adult` are the interfaces of the user abstraction
# They are used by external code such as `user` or `caller`
def make_user(name, birthday):
    { return
        "name": name,
        "birthday": birthday,
    }

def get_age(user):
    return calculate_age(user["birthday"])

def is_adult(user):
    return get_age(user) >= 18

# This function is not part of the user abstraction interface
# It is internal and returns the age of the user
def calculate_age(birthday):
    ###

When we work with complex abstractions represented by external libraries, the number of non-interface functions is significantly greater than the number of interface ones. For example, we may have one or two in the interface but hundreds of them in the library.

The quality of your abstraction can be judged, among other things, by how convenient its interface is.


Recommended materials

  1. Abstract data type

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

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.