filmov
tv
Encapsulation in Python

Показать описание
#pyhton #oop #objectorientedprogramming
In this lecture, we will explore the concept of encapsulation in Python.
We'll cover the different types of attributes, including public, protected, and private, and discuss how encapsulation helps to safeguard our data.
We'll also dive into getter and setter methods, and how we can use the @property decorator to make our code cleaner.
By the end of this lecture, you'll see how encapsulation improves the structure and integrity of our code.
Encapsulation is one of the main pillars of Object-Oriented Programming.
It's all about bundling data (or attributes) and the methods that operate on that data into a single unit, usually a class.
With encapsulation, we can hide the internal state of objects from the outside, controlling how it's accessed and modified.
This is especially useful for maintaining the integrity of our data.
Now, let's dive into how we handle different types of attributes in Python.
In Python, naming conventions help us distinguish between public, protected, and private attributes, signaling how they should be accessed and used within our code.
These conventions are more about guiding developers than strictly enforcing rules.
Inside this file, we'll define a simple Person class.
Here's what we're doing in this code:
First, we define a Person class with an __init__ method, which initializes three attributes: name, _age, and __salary.
self._age is a protected attribute. The single underscore (_) before the name is a convention in Python.
It's a way of saying: This attribute is intended for internal use only, within the class and its subclasses.
However, this is not a strict rule. Python does not prevent us from accessing or modifying _age from outside the class.
It's more like a warning label for other developers, telling them to be cautious.
self.__salary is a private attribute, indicated by double underscores.
This is meant to be hidden from outside access, even from subclasses.
If we run this code, nothing is printed out yet, as we're just defining the class.
To see this in action, let's create an instance of Person and try to access these attributes:
When we run this code,
We observe: We can access 'name' directly because it's a public attribute.
We can also access '_age' directly, even though it's marked as 'protected.'
This is because the single underscore is just a convention. It doesn't strictly restrict access.
We get an AttributeError when trying to access '__salary' because double underscores make it a private attribute.
This is due to Python's name mangling mechanism.
To sum up, the single underscore (_) suggests a convention to developers that certain attributes are 'protected' and meant for internal use.
In contrast, double underscores (__) actually restrict access by using name mangling, making it harder to access private attributes from outside the class.
By using double underscores (__), Python applies a process called name mangling.
This means that Python automatically changes the name of the attribute to include the class name as a prefix.
For example, if we have an attribute named '__salary' in a class called Person, Python internally changes this name to '_Person__salary'.
This makes it much harder to access the attribute directly from outside the class because the original name __salary is no longer used.
This name mangling helps protect the attribute and avoid accidental access or modification from outside the class.
However, it's still possible to access the attribute if we know the mangled name, but it requires an intentional effort to do so.
Let's try it out just for fun. I'll comment out this problematic line to allow the code to run to the end without interruption.
If we run the script again,
We will see that it now prints the salary using the mangled name.
While it is technically possible to access the private attribute using its mangled name, doing so goes against the intention of keeping the attribute private.
This is why accessing private attributes in this way is not recommended.
Instead, to safely access and modify private attributes, we use getter and setter methods.
Let's add these methods to our Person class:
Here's what we're doing now:
We added a 'get_salary' method that returns the value of '__salary'.
The 'set_salary' method updates the '__salary' value but only if the provided salary is positive.
This adds an extra layer of control, ensuring that the salary cannot be set to an invalid value.
Let's use these methods:
When we run this code,
We should see the salary printed, updated, and then an error message for the invalid input.
Using getter and setter methods works well, but there's an even more Pythonic way to handle access to private...
In this lecture, we will explore the concept of encapsulation in Python.
We'll cover the different types of attributes, including public, protected, and private, and discuss how encapsulation helps to safeguard our data.
We'll also dive into getter and setter methods, and how we can use the @property decorator to make our code cleaner.
By the end of this lecture, you'll see how encapsulation improves the structure and integrity of our code.
Encapsulation is one of the main pillars of Object-Oriented Programming.
It's all about bundling data (or attributes) and the methods that operate on that data into a single unit, usually a class.
With encapsulation, we can hide the internal state of objects from the outside, controlling how it's accessed and modified.
This is especially useful for maintaining the integrity of our data.
Now, let's dive into how we handle different types of attributes in Python.
In Python, naming conventions help us distinguish between public, protected, and private attributes, signaling how they should be accessed and used within our code.
These conventions are more about guiding developers than strictly enforcing rules.
Inside this file, we'll define a simple Person class.
Here's what we're doing in this code:
First, we define a Person class with an __init__ method, which initializes three attributes: name, _age, and __salary.
self._age is a protected attribute. The single underscore (_) before the name is a convention in Python.
It's a way of saying: This attribute is intended for internal use only, within the class and its subclasses.
However, this is not a strict rule. Python does not prevent us from accessing or modifying _age from outside the class.
It's more like a warning label for other developers, telling them to be cautious.
self.__salary is a private attribute, indicated by double underscores.
This is meant to be hidden from outside access, even from subclasses.
If we run this code, nothing is printed out yet, as we're just defining the class.
To see this in action, let's create an instance of Person and try to access these attributes:
When we run this code,
We observe: We can access 'name' directly because it's a public attribute.
We can also access '_age' directly, even though it's marked as 'protected.'
This is because the single underscore is just a convention. It doesn't strictly restrict access.
We get an AttributeError when trying to access '__salary' because double underscores make it a private attribute.
This is due to Python's name mangling mechanism.
To sum up, the single underscore (_) suggests a convention to developers that certain attributes are 'protected' and meant for internal use.
In contrast, double underscores (__) actually restrict access by using name mangling, making it harder to access private attributes from outside the class.
By using double underscores (__), Python applies a process called name mangling.
This means that Python automatically changes the name of the attribute to include the class name as a prefix.
For example, if we have an attribute named '__salary' in a class called Person, Python internally changes this name to '_Person__salary'.
This makes it much harder to access the attribute directly from outside the class because the original name __salary is no longer used.
This name mangling helps protect the attribute and avoid accidental access or modification from outside the class.
However, it's still possible to access the attribute if we know the mangled name, but it requires an intentional effort to do so.
Let's try it out just for fun. I'll comment out this problematic line to allow the code to run to the end without interruption.
If we run the script again,
We will see that it now prints the salary using the mangled name.
While it is technically possible to access the private attribute using its mangled name, doing so goes against the intention of keeping the attribute private.
This is why accessing private attributes in this way is not recommended.
Instead, to safely access and modify private attributes, we use getter and setter methods.
Let's add these methods to our Person class:
Here's what we're doing now:
We added a 'get_salary' method that returns the value of '__salary'.
The 'set_salary' method updates the '__salary' value but only if the provided salary is positive.
This adds an extra layer of control, ensuring that the salary cannot be set to an invalid value.
Let's use these methods:
When we run this code,
We should see the salary printed, updated, and then an error message for the invalid input.
Using getter and setter methods works well, but there's an even more Pythonic way to handle access to private...