Double underscore (__) in front of a variable is a convention. It is used for global variable (The following variables may appear to be global but are not, rather local to each module) in Nodejs meanwhile Underscore(_) used to define private variable.
Double underscores are used for fully private variables. If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.
A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.
The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.
Sometimes it is used in order to distinguish class members from local variables:
public class MyClass {
private int _salary;
public MyClass(int salary) {
_salary = salary
}
}
However, you should follow Java Naming Conventions that doesn't recommend using that. You can simply name the class member without the leading _
and do:
this.salary = salary;
The use of " _ " (underscore) must be evaluated carefully.
Use " _ " to indicate class attributes is used a lot in C + +.
In Java, it is not necessary because the language has the keyword this
.
An underscore in front usually indicates a instance variable as opposed to a local variable. It's merely a coding style that can be omitted in favor of "speaking" variable names and small classes that don't do too many things.
this should be an idiomatic usage, and describe it is a private variable. also this is a way to define private method in python to add the "_" as the method prefix like def _privateMethodName(self):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With