1. _var(single underscore)
This is a hint to programmer, it means the variable for internal use. It is just a hint, not enforce by Python.
From the example, we can see that although _bar for internal use, we can also get it from outside. Because it is just a hint, not enforce by Python.
2. var_(single underscore)
Sometimes, we already use a suitable name for others or some keyword we can not use as a variable name, so we can user
var_ instead of it.
For the instance, we can not use
class as a variable name, but we can use
class_ as a variable name.
3. __var(double underscore)
A double underscore prefix cause Python rewrite the attribute name. This is also called name mangling, the Python interpreter change variable name in order to make it harder create collisions when the class is extended later, it also looks like private attribute.
From the example, we can see that variables name with double underscore prefix changed to another name inside, and we can not get the variable with the name we assigned to it, we should use the name which the Python interpreter assign to it. But, the amazing thing is our code can use the name we assigned inside of the class. Let\’s get another example:
4. __var__(double underscore)
Name Mangling doesn\’t apply for variable name starts and ends with double underscore. Variables name start and end with double underscore means the variable is constructor(like __init__) or can be call by outside(like public attribute). Normally, we don\’t use start and end double underscore to name our variables.
5. _ (just a single underscore)
_ is just temporary variables, and we don’t actually use it. it looks like placeholder variables.
Reference: The Meaning of Underscores in Python