Private Variables in Python

There are three variable scopes you need to know. There are global variables, public variables, and private variables. Global variables can be accessed from all functions and modules in a program. Ideally they are only declared once in a whole program. Public variables belong to certain classes, but are visible to other classes, functions, and modules. Private variables belong to certain classes and are only visible to that class.

In this post we’ll learn about private variables and how to use them in Python. We will cover:

  • What a private variable is
    • When to use a private variable
    • How to define a private variable in Python
  • A summary of private variables in Python

What is a Private Variable?

As we stated above, private variables are variables that belong to a class and are only visible to that class. This means that private variables can be manipulated inside of a class, and inside of that class only. They are inaccessible outside of their respective class. When a class is instantiated into an object, only functions belonging to that object can touch the variable. 

In programming languages like Java and C/C++, private variables are explicitly declared as private. Python does not have this designation, in fact Python requires no modifiers when instantiating a variable. So, how does one make a private variable in Python? With the use of double underscores. Before we learn exactly how to declare and use private variables in Python, let’s first learn when we should use private variables.

When to Use Private Variables

Private variables are only accessible inside of the classes or objects that they are part of. They cannot be seen outside of their scope of declaration unless exposed by a non-private function. This means that, by default, private variables do not allow dependencies. This makes for easier debugging and a smaller stack trace. 

These features actually make private variables the ideal default state for variables. However, Python is a “pass by name” language, meaning that all variables are public by default. In any case where a variable does not need to be publicly accessible, they should be private. So, let’s take a look at how we can declare variables as private in Python.

How to Define a Private Variable in Python

A single underscore indicates to the editor that a variable should not be touched. However, neither single underscore prefixes nor suffixes actually enforce usage restrictions to the interpreter. Python enforced private variables through the double underscore prefix. The double underscore prefix tells the Python interpreter to “mangle” the name of the variable. Since Python is a pass by name language, changing the name of the variable renders it inaccessible outside of the function and therefore private.

Let’s take a look at an example below.

class Shape:
    def __init__(self, sides):
        self.sides = sides
   
    def name(self, name):
        self.__name = name
   
    def get_name(self):
        return self.__name
 
triangle = Shape(3)
triangle.name("t1")
print(triangle.get_name())
print(triangle.sides)
print(triangle.__name)

The above code should give the following output. Notice how we can access the sides variable but not the __name variable. The only way we can access private variables is through exposing them using a public function as I did with get_name().

Private variables can't be accessed outside of scope
Private variables can’t be accessed outside of scope

Summary of Private Variables in Python

In this post we briefly discussed the three types of variables: global, public, and private. Private variables should be the default for any variable we don’t need access to outside of a class. We learned that while many languages have modifiers on variable declaration for public and private variables, Python does not. This means that we have to use a special notation to declare private variables in Python. The Python specific notation to declare a variable private is the double underscore prefix. Finally, we looked at an example of declaring a private variable in Python and how we could access that variable indirectly.

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
Yujian Tang

One thought on “Private Variables in Python

Leave a Reply

%d bloggers like this: