Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use one or two underscore in Python [duplicate]

Ok I think I have understood the use of one and two heading underscores in Python.

Correct me if I am wrong,

  1. In the case of one underscore, the underscore prevents the from X import * statement to import this kind of variables.

  2. In the case of two underscores, the variable's name is prepended with the name of the class it belongs to allow a higher level of "privateness".

My question now is: why not use two underscores only? In which cases one underscore is preferred (or needed) over two underscores?

like image 866
aretor Avatar asked Jan 20 '17 10:01

aretor


People also ask

What is difference between _ and __ in Python?

Double Leading Underscore __var : Triggers name mangling when used in class context. Enforced by the Python interpreter. Single Trailing Underscore var_ : Used by convention to avoid naming conflicts with Python keywords. Double Trailing Underscore __var__ : Indicates special methods defined by Python language.

What which are different between __ double underscore and single underscore on 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.

Why do we use __ in Python?

1. Use in Interpreter. Python automatically stores the value of the last expression in the interpreter to a particular variable called "_." You can also assign these value to another variable if you want.

What do 2 underscores mean in Python?

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.


1 Answers

Short answer: use a single leading underscore unless you have a really compelling reason to do otherwise (and even then think twice).

Long answer:

One underscore means "this is an implementation detail" (attribute, method, function, whatever), and is the Python equivalent of "protected" in Java. This is what you should use for names that are not part of your class / module / package public API. It's a naming convention only (well mostly - star imports will ignore them, but you're not doing star imports anywhere else than in your Python shell are you ?) so it won't prevent anyone to access this name, but then they're on their own if anything breaks (see this as a "warranty void if unsealed" kind of mention).

Two underscores triggers a name mangling mechanism. There are very few valid reason to use this - actually there's only one I can think of (and which is documented): protecting a name from being accidentally overridden in the context of a complex framework's internals. As an example there might be about half a dozen or less instances of this naming scheme in the whole django codebase (mostly in the django.utils.functional package).

As far as I'm concerned I must have use this feature perhaps thrice in 15+ years, and even then I'm still not sure I really needed it.

like image 145
bruno desthuilliers Avatar answered Oct 04 '22 01:10

bruno desthuilliers