Introduction to Abstraction
Abstraction is the process of simplifying complex concepts or systems by focusing on the most important aspects while hiding unnecessary details. It involves creating higher-level representations or models that capture the essential elements or behaviors, allowing for easier understanding, communication, and problem-solving.
Abstraction in programming can be achieved through a number of techniques including:
- Data Abstraction
- Function Abstraction
Data abstraction involves representing complex data structures or entities in a simplified manner by encapsulating them into classes or objects. It allows programmers to define abstract data types (ADTs) that hide the implementation details and expose only essential operations or behaviors.
Python contains a number of in-built data structures, such as lists and dictionaries, that abstract away the unnecessary details and complexity. In the example below we are only storing the name of the animal, because all other details are irrelevant.
The specific details of the implementation are hidden – for example we don’t know (and don’t care) where in memory the data is stored, or how it is encoded.
Encapsulation is the practice of bundling data and related operations together into a single unit, typically known as a class. It allows for information hiding, where the internal details of an object or module are inaccessible to external entities, promoting modularity and preventing unintended dependencies.
Function abstraction involves encapsulating a set of instructions or operations into a function or method. It allows for code reuse, improved readability, and separation of concerns by providing a named abstraction for a specific task or behavior.
Abstraction through Modularisation
Modularization involves breaking down a program into smaller, independent modules or components. Each module focuses on a specific task or responsibility, hiding the internal details and providing well-defined interfaces for interaction with other modules. This promotes code reusability, maintainability, and scalability.
In the example above the code responsible for calculating the area and circumference of a circle is moved to its own module. This makes the main.py code more readable as well as providing other benefits.