This content originally appeared on DEV Community and was authored by I'm Just a Guy with a Computer
encapsulation is a method of wrapping data and functions into a single entity.Acts as a protective layer which means internal representation of an object is generally hidden from outside of the object's definition.
class Employee:
def __init__(self,name,salary):
#private
self.name = name
#private member
# not accessible outside of a class
self.__salary = salary
def show(self):
print("Name is", self.name, "and salary is", self.__salary)
emp = Employee("Jessa", 10000)
emp.show()
# access salary from outside of a class
print(emp.__salary)
output
Name is Jessa and salary is 10000
AttributeError: 'Employee' object has no attribute '__salary'
This content originally appeared on DEV Community and was authored by I'm Just a Guy with a Computer

I'm Just a Guy with a Computer | Sciencx (2025-01-13T18:45:42+00:00) Encapsulation in Python. Retrieved from https://www.scien.cx/2025/01/13/encapsulation-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.