Private variables in Python

There are no "private" instance variables in Python that cannot be accessed except inside an object. However, most Python code and coders follow a convention, i.e. A name prefixed with an underscore. For example, _geek should be considered a non-public part of the API or any Python code, be it a function, method, or data member. Going through this, we would also try to understand the concept of various forms of trailing

# Python code to illustrate how distortion works

class Map :

def __ init __ ( self , iterate):

  self . list = []

  self .__ geek (iterate)

def geek ( self , iterate):

for item in iterate:

self . list . append (item)


  # personal copy of the original geek () method

__ geek = geek 


class MapSubclass ( Map ):


# provides a new signature for geek (), but

# does not break __init __ ()

  def geek ( self , key, value): 

for i in zip (keys, values):

self . list . append (i)

The mangle rules are designed primarily to avoid accidents, but it is still possible to access or change a variable that considered private. It can even be useful in special circumstances such as a debugger.

_Single leading underscores

So one underscore at the beginning of a method, a function or data member means that you shouldn`t refer to this method because it is not part of the API. Let`s take a look at this piece of code:

# Python code for illustration
# how one works underline

def _ get_errors ( self code> ):

if self ._ errors is None :

self . full_clean ()

return self ._ errors


 errors = property (_ get_errors)

Snippet taken from Django source code (django / forms / forms.py). This suggests that errors are a property and also part of the API, but the _get_errors method is "private" so it should not be accessed.

__Double underscores

The two underscores at the beginning cause a lot of confusion. It`s more about syntax than convention. double underscores will mangle the attribute names of the class to avoid attribute name conflicts between classes. For example:

# Python code illustrating doubling
# underline at the beginning of work

class Geek:

def _ single_method ( self ):


def __ double_method ( self ): # to distort


class Pyth (Geek):

  def __ double_method ( self ): # to distort


__Double underscores and double trailing underscores__

There is another case of double leading and trailing underscores. We follow this when using special variables or methods (called "magic method") such as __len__, __init__. These methods provide special syntax for names. For example, __file__ specifies the location of the Python file, __eq__ is executed when the expression == b is executed. 

# Python code to illustrate double leading and
# double underscore closure

class Geek:


# & # 39; __ init __ & # 39; for initialization, this is

# special method

def __ init __ ( self , ab): 

self . ab = ab


# special custom method. try not to use it

def __ custom__ ( self ): 


This article provided by Chinma Lenka . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks. 
I have referred to the Python docs, hackernoon.com and igorsobreira.com

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Get Solution for free from DataCamp guru