What are Python Private Variables?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The term python private variables refer to those variable values that have the scope within a class, i.e., these variables can be viewed and accessed within the class in which they have been declared and the classes outside that class cannot access them.

Note: Python's private variables are just a syntactic notion and not an enforced implementation.

Python does not provide any private keyword or instance to define python private variables. In python, a private variable is defined by defining a variable and prefixing its name with a single underscore (_).

, For example,, in the following case, the variable (phoneNumber) is a python private variable.

What is Mangling?

Python provides limited support in the case of valid use cases for private class members. This is done to prevent the clashing of the names with the names defined by any sub-class. This is known as mangling in python.

The rules for mangling in python are designed in such a way that it should prevent accidents but at the same time, it has the accessibility for the modification of a private variable.

How Mangling Works?

In Python, the mangling works by support for valid use cases in the case of private class members. This is done to restrict the clash of the provided names with any subclass's defined name.

Suppose we have an identifier __example (consisting of two leading underscores). Then this identifier __example will be appended with _classname, thus, the identifier becomes _classname__example, where the class name denotes the name of the current class name suffixed with two leading underscores. The mangling is considered done till this _classname__example is present within the class definition. This process helps the subclasses in overriding the methods while avoiding the breaking of intraclass method calls.

What are _Single Leading Underscores?

The _Single Leading Underscores in python resembles that the object is being used internally. The single underscore (_) is usually prefixed against a variable name, a method, or a function. The single leading underscores are used to implement Python private variables.

The addition of a single leading underscore in python is considered less of a functionality and more of a syntactical symbol suggesting to the programmer that these objects still have accessibility from another script in one way to the other. This was defined as convection in PEP 8.

Syntax

What are __Double Leading Underscores?

The Double leading underscores in Python is used to make the Python interpreter rewrite the names of the attributes in a manner to prevent conflicts among subclass names.

This process of the Python interpreter changing the variable names in a manner that prevents the collision among names upon class extension is also known as name mangling the interpreter.

Syntax

What are Double Leading and Double Trailing Underscores?

In Python, when a variable is both prefixed and suffixed with double underscores (__), then name mangling does not apply to that variable. The Python interpreter leaves the variables with double leading and double trailing underscores unscathed.

The names that have both leading and trailing double underscores are usually reserved for special use in the language. This rule covers things like __init__ for object constructors, or __call__ to make an object callable.

The double leading underscores and double trailing underscores in Python are used for magic methods.

Syntax

How to Create Python Private Variables?

As discussed earlier, Python Private variables are the members that are only available within their class and cannot be accessed by the subclasses or any other class within the same package.

Python does not provide any strict enforcement for the implementation of Python Private Variables. Unlike other programming languages, Python does not contain access specifiers for making any variable or function private.

Although, Python provides the feature of name mangling to impose some privacy and restrictions on methods. In this section, we will learn how to implement Python private variables.

Example

In this example, we will implement a class with a private variable and will try to access that Python private variable. In this example, we will be emulating the private variables' behavior by using mangling.

Code

Output:

Explanation of the example

In the above example, we have created a class SavingAccount that contains the variable amount and a method getAmount. The __init__(self, amount) method will be used to initiate the class object. We are declaring the amount variable as a python private variable using leading double underscores. The user is an object of the SavingAccount class and is initiated with a value "10,000".

When the user.getAmount() method is called then the user is calling the getAmount method which gets access to the private variable amount within the class and thus, displays "Current amount -> 10000".

When the user.__amount method is called then the user is calling the private variable amount outside the class thus, displays "AttributeError: object has no attribute '__amount'"

Learn more

Conclusion

  • Python private variables refer to a variable that can be viewed and accessed within the class.
  • Python does not provide any strict enforcement for the implementation of Python Private Variables.
  • Python does not contain access specifiers for making any variable or function private.
  • Python provides the feature of name mangling to impose some privacy and restrictions on methods.
  • In Python, mangling works by providing a limited amount of support for the valid use cases in the case of private class members.
  • Mangling is implemented to restrict the clash of the provided names with any subclass's defined name.
  • The Single Leading Underscores in python resemble that the object is being used internally
  • The Double leading underscores in Python are used to prevent naming conflicts among subclasses.
  • When a variable is both prefixed and suffixed with double underscores (__), then name mangling does not apply to that variable.