filmov
tv
#9 Encapsulation in python|| Name Mangling||Get& set methods|python for beginners||In Telugu|python3

Показать описание
#encapsulation in python#python#python encapsulation#what is encapsulation in python#encapsulation in python in hindi#encapsulation#object oriented programming in python#how to use encapsulation in python#python encapsulation examples#python tutorial#oop in python#encapsulation python#encapsulation in python in telugu#encapsulation example in python#python programming#learn python#python encapsulation program#functions in python#python for beginners #name mangling in python #python#name mangling
#python name mangling#python tutorial#learn python#private variables in python#python class name mangling#python name mangling example#data mangling in python#python for beginners#name mingling in python#mingling in python#what is name mangling in python#oop in python##concept of name mangling in python#object oriented programming in python#private methods in python#python 3#python in petroleum
--------------------------------------------------------------------------------------------------------------
0:00 - Introduction
0:36 - What is Encapsulation?
2:04 - Advantages of Encapsulation
6:08 - Access Modifiers
14:57 - Name Mangling
17:01 - Getter & Setter methods
----------------------------------------------------------------------------
Encapsulation in Python is a fundamental concept of object-oriented programming (OOP) that refers to the bundling of data (attributes) and methods (functions) that operate on that data into a single unit, typically a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental interference and misuse of the data.
### Key Aspects of Encapsulation:
1. **Class Definition**: A class in Python is used to define a blueprint for creating objects. It encapsulates data and functions together.
```python
class Car:
def __init__(self, make, model, year):
```
2. **Private and Public Members**: In Python, attributes and methods are by default public, meaning they can be accessed from outside the class. However, you can define private members by prefixing them with an underscore `_` or double underscore `__`.
```python
class Car:
def __init__(self, make, model, year):
self.__make = make # private attribute
self._model = model # protected attribute
def get_make(self): # public method
return self.__make
def _get_model(self): # protected method
return self._model
```
3. **Getter and Setter Methods**: These methods are used to access and update the value of private attributes. This helps in controlling how the data is modified or accessed.
```python
class Car:
def __init__(self, make, model, year):
self.__make = make
def get_make(self):
return self.__make
def set_make(self, make):
self.__make = make
```
4. **Encapsulation Benefits**:
- **Data Hiding**: Encapsulation hides the internal state of the object from the outside. This means that the internal representation of an object is hidden from the outside, only exposing a limited interface.
- **Modularity**: Each class can be developed independently, and the internal details of the implementation can be changed without affecting other parts of the code.
- **Control**: Encapsulation allows the control of how data is accessed or modified. This prevents accidental or unauthorized access and modification.
- **Maintainability**: Code is more maintainable and less prone to errors because the data is not exposed to the entire program.
### Example:
```python
class Employee:
def __init__(self, name, salary):
self.__name = name # private attribute
self.__salary = salary # private attribute
def get_name(self): # getter method
return self.__name
def set_name(self, name): # setter method
self.__name = name
def get_salary(self): # getter method
return self.__salary
def set_salary(self, salary):# setter method
if salary 0:
self.__salary = salary
else:
raise ValueError("Salary must be positive")
# Using the class
emp = Employee("John Doe", 50000)
```
In this example, the `Employee` class encapsulates the name and salary attributes, providing controlled access through getter and setter methods. This ensures that the salary cannot be set to a negative value, demonstrating the power of encapsulation to enforce rules and protect the integrity of the data.
#python name mangling#python tutorial#learn python#private variables in python#python class name mangling#python name mangling example#data mangling in python#python for beginners#name mingling in python#mingling in python#what is name mangling in python#oop in python##concept of name mangling in python#object oriented programming in python#private methods in python#python 3#python in petroleum
--------------------------------------------------------------------------------------------------------------
0:00 - Introduction
0:36 - What is Encapsulation?
2:04 - Advantages of Encapsulation
6:08 - Access Modifiers
14:57 - Name Mangling
17:01 - Getter & Setter methods
----------------------------------------------------------------------------
Encapsulation in Python is a fundamental concept of object-oriented programming (OOP) that refers to the bundling of data (attributes) and methods (functions) that operate on that data into a single unit, typically a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental interference and misuse of the data.
### Key Aspects of Encapsulation:
1. **Class Definition**: A class in Python is used to define a blueprint for creating objects. It encapsulates data and functions together.
```python
class Car:
def __init__(self, make, model, year):
```
2. **Private and Public Members**: In Python, attributes and methods are by default public, meaning they can be accessed from outside the class. However, you can define private members by prefixing them with an underscore `_` or double underscore `__`.
```python
class Car:
def __init__(self, make, model, year):
self.__make = make # private attribute
self._model = model # protected attribute
def get_make(self): # public method
return self.__make
def _get_model(self): # protected method
return self._model
```
3. **Getter and Setter Methods**: These methods are used to access and update the value of private attributes. This helps in controlling how the data is modified or accessed.
```python
class Car:
def __init__(self, make, model, year):
self.__make = make
def get_make(self):
return self.__make
def set_make(self, make):
self.__make = make
```
4. **Encapsulation Benefits**:
- **Data Hiding**: Encapsulation hides the internal state of the object from the outside. This means that the internal representation of an object is hidden from the outside, only exposing a limited interface.
- **Modularity**: Each class can be developed independently, and the internal details of the implementation can be changed without affecting other parts of the code.
- **Control**: Encapsulation allows the control of how data is accessed or modified. This prevents accidental or unauthorized access and modification.
- **Maintainability**: Code is more maintainable and less prone to errors because the data is not exposed to the entire program.
### Example:
```python
class Employee:
def __init__(self, name, salary):
self.__name = name # private attribute
self.__salary = salary # private attribute
def get_name(self): # getter method
return self.__name
def set_name(self, name): # setter method
self.__name = name
def get_salary(self): # getter method
return self.__salary
def set_salary(self, salary):# setter method
if salary 0:
self.__salary = salary
else:
raise ValueError("Salary must be positive")
# Using the class
emp = Employee("John Doe", 50000)
```
In this example, the `Employee` class encapsulates the name and salary attributes, providing controlled access through getter and setter methods. This ensures that the salary cannot be set to a negative value, demonstrating the power of encapsulation to enforce rules and protect the integrity of the data.
Комментарии