Encapsulation in Python

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__(…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Encapsulation in Python." I'm Just a Guy with a Computer | Sciencx - Monday January 13, 2025, https://www.scien.cx/2025/01/13/encapsulation-in-python/
HARVARD
I'm Just a Guy with a Computer | Sciencx Monday January 13, 2025 » Encapsulation in Python., viewed ,<https://www.scien.cx/2025/01/13/encapsulation-in-python/>
VANCOUVER
I'm Just a Guy with a Computer | Sciencx - » Encapsulation in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/13/encapsulation-in-python/
CHICAGO
" » Encapsulation in Python." I'm Just a Guy with a Computer | Sciencx - Accessed . https://www.scien.cx/2025/01/13/encapsulation-in-python/
IEEE
" » Encapsulation in Python." I'm Just a Guy with a Computer | Sciencx [Online]. Available: https://www.scien.cx/2025/01/13/encapsulation-in-python/. [Accessed: ]
rf:citation
» Encapsulation in Python | I'm Just a Guy with a Computer | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.