Python Encapsulation

What is encapsulation in a nutshell?

    In Object Oriented Programming, sometimes we need to put some restrictions. We may have some variable that we don’t want to change its value but there is possibility that we can change into the code by mistake.

     With using Encapsulation, we can prevent this risk.


     To have a ‘private’ variable or a function, we need to use double underscores (__) such as “__pi_number” or “__welcome()”.

Conclusion: Thus, we can only use these variable or functions into the class.

IMPORTANT HINT = Actually we can reach private variables or functions from out of class with;


          => {instance}._{CLASSNAME}{VARIABLENAME or FUNCTION NAME}

          => Example;           calc._Calculation__welcome()

          But the point of encapsulation, preventing mistakes. So you already would not run a command like that.


I tried to explain with a simple code;

Please find in here ;

#INFO = There is no copy/paste information into this file.
#Author:       Ahmet Gungor
#Date  :       17.10.2019
#Description : This code is written to understand Encapsulation concept in Python.

class Calculation:

    def __init__(self):
        # public variable
        self.value = 5
        # private variable
        self.__pi_number =  3.14
        # Call private __welcome() function
    # private function
    def __welcome(self):
        print("Welcome to the Calculation class")
    # public function
    def goodbye(self):
        print("See you later")
if __name__ == "__main__":
    calc = Calculation()
    # Output => Welcome to the Calculation class
    # Into constructor method __welcome() function is called.
    # The function is worked because it is called into its own class.
    # Output => 5
    calc.value = 10
    # Output => 10
    # Output => AttributeError: 'Calculation' object has no attribute '__pi_number'
    # Because __pi_number is a private variable.
    # Output => AttributeError: 'Calculation' object has no attribute '__welcome'
    # Because __welcome() is a private function
    # Output => Welcome to the Calculation class
    # With this special usage, you can reach to variable or function.
    # You may have exceptional case to reach value or function somehow. 
    # That's the way. Still not recommended
    # Output => See you later

See ya!