Free Ebook cover Python course with Lambda and API Gateway for backend development

Python course with Lambda and API Gateway for backend development

5

(1)

142 pages

Object-Oriented Programming in Python: Encapsulation in Python

Capítulo 23

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Object-oriented programming (OOP) is a programming paradigm that offers a means of structuring a software program around objects and data, rather than functions and logic. An object is a thing or concept that can be clearly defined. In OOP, each object is an instance of a class. Python is a programming language that supports object-oriented programming and one of the main features of OOP is encapsulation.

6.4. Object-Oriented Programming in Python: Encapsulation in Python

Encapsulation is one of the main characteristics of object-oriented programming. It refers to the practice of hiding the internal details of how an object works and exposing only the interfaces through which that object can be used. In other words, encapsulation is a way of hiding the implementation details of an object and exposing only the methods and properties necessary to interact with that object.

In Python, encapsulation is performed using private and protected methods and variables. A private variable is a variable that cannot be accessed or modified directly outside the class where it is defined. A protected variable is similar to a private variable, but can be accessed and modified in subclasses.

To declare a variable as private in Python, we prefix the variable name with two underscores (__). For example, __variable_name. To declare a variable as protected, we prefix the variable name with an underscore (_). For example, _variable_name.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Encapsulation in Python is not as strict as in other object-oriented programming languages, such as Java or C++. In Python, it's more of a convention than a hard-and-fast rule. However, it is good practice to follow this convention to make the code easier to understand and maintain.

Let's consider the following example of encapsulation in Python:

class ExampleEncapsulation:
    def __init__(self):
        self.__private_variable = "I am a private variable"
        self._variable_protected = "I am a protected variable"

    def get_private_variable(self):
        return self.__private_variable

    def get_protected_variable(self):
        return self._protected_variable

In this example, we create a class called EncapsulationExample. Within this class, we define two variables: a private variable (__private_variable) and a protected variable (_protected_variable). We also define two methods to obtain the values ​​of these variables.

If we try to access the private variable directly outside the class, we will get an error. However, we can access the value of the private variable using the get_private_variable() method. Likewise, although we can access the protected variable directly outside the class, it is good practice to access it using the get_protected_variable() method.

Encapsulation in Python helps us make our code more secure and easier to maintain. It allows us to hide the implementation details of an object and expose only the interfaces necessary to interact with that object. This makes the code easier to understand and less prone to errors.

Now answer the exercise about the content:

What is encapsulation in object-oriented programming and how is it implemented in Python?

You are right! Congratulations, now go to the next page

You missed! Try again.

Encapsulation in object-oriented programming is about hiding the internal details of how an object works, exposing only necessary interfaces. In Python, this is implemented using private and protected methods and variables. Private variables, prefixed with __, cannot be accessed directly outside the class, while protected variables, prefixed with _, can be accessed in subclasses.

Next chapter

Object-Oriented Programming in Python: Special Methods in Python

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.