Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the double underscore, after a single underscore, as a parameter to a function / class method?

In my exploration of Polymer Dart 1.0.0, I've found for events, and observer methods, I am forced to use this pattern

@reflectable
void someEvent([_, __]) {
    ...
}

or on an observer method

@Observe('someField')
void someFieldChanged([_, __]) {
    ...
}

I understand what the square brackets are for, optional parameters, I also understand that if you don't care about the passed parameters, you can represent this parameter with the underscore. What surprised me was the examples I looked at used double underscore, __, as the second symbol between the square brackets. When I tried to use just a single underscore again, I get a duplicate formal parameter error. Is there some reason why the second parameter you don't care about has to be different from the first? By this logic, if I include a third one, does it mean it'll have to be a triple underscore ___?

Thanks.

like image 919
James Hurford Avatar asked Oct 15 '15 10:10

James Hurford


People also ask

Why do we use double underscore?

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.

What does _ and __ mean in Python?

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.

What's the meaning of single and double underscores in Python?

Single Trailing Underscore( var_ ): Used by convention to avoid naming conflicts with Python keywords. Double Leading Underscore( __var ): Triggers name mangling when used in a class context. Enforced by the Python interpreter.

What does __ mean in Python functions?

The Python interpreter modifies the variable name with ___. So Multiple times It uses as a Private member because another class can not access that variable directly. The main purpose for __ is to use variable /method in class only If you want to use it outside of the class you can make it public.


1 Answers

Nothing special. _ as __ as a are just variable identifiers. _ is often used to name an unused variable. Here there are 2 variables unused, the first one is named _ and the second one __. With multiple unused variables it's common to name them _, __, ___ ... or _1,_2,_3...

like image 67
Alexandre Ardhuin Avatar answered Nov 15 '22 09:11

Alexandre Ardhuin