Underscore | What is the Meaning of Single and Double Underscore Before an Object Name?

In this post, we will learn more about What is the meaning of single and double underscores before an object name. where each single and double have different use in python which we will discuss below with example to explain all these and get a clear idea of both. 


Single Underscore

As we know a single underscore is also used for different uses and we have explained it all here.

It is used for hints or simply to indicate the object as private or, to make it different from the objects either method or variable is not part of public objects It can also make certain differences in between private and public objects of API of a class or module.

It helps in making the object private as another programmer could not access it directly, Moreover, it is simply a naming convention, In addition, the object can access it from outside the class or module. And we can also use a single underscore for naming a variable in between two words where we need to use a space in general language but the variable name could not have space in between so we simply use a single underscore to avoice the space as the space is not supported in Python variable naming.

Double Underscore

There are different uses of double underscore which we has listed here all the uses of it.

It is used for trigger name mangling, which could help in avoiding the naming conflict in the subclasses. In addition, the interpreter renames the object with a prefix of the name of the class for example __classname which helps in many ways as it helps in avoiding the overridden accidentally by making it harder it to make the same by using double underscores.

Even after making the changes i the name of the object by adding double underscores, we can still access it using the Mangled name which is nothing but __classname __objectname. And it is also used as a special method by applying double underscores.

Here we have given an example showing the use of single and double underscores here.

class Test:
    def __init__( self):
        self.public_var = "I am public"
        self._single_underscore_var = "I am supposed to be private"
        self.__double_underscore_var = "I am also private, but with name mangling"
    def print_vars( self):
        print( self.public_var)
        print( self._single_underscore_var)
        print( self.__double_underscore_var)
test = Test()

In above code where the class test with three variables is used and the variables are as public_var, _single_underscore_var, __Double_Undercore_Variable.



For learning more about What is the meaning of single and double underscores before an object name Visit: by Stack overflow

To learn more about python solutions to different python problems and tutorials for the concepts we need to know to work on python programming along with different ways to solve any generally asked problems: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?.

Leave a Comment

%d bloggers like this: