Understanding practical usage of __ (double underscore) in python

A.J.

I saw functions returning tuples and at receiving end, sometimes I see syntax something like this.

def foo():
    return (True, False)

foo, __ = foo()

The thing I investigated is that, __ (double underscore) don't give pep8 error as if its not being used anywhere.

Seeking:

  1. the practice situation where we should use this
  2. Situations we should avoid
  3. Any alternatives.
rmunn

As Andrés Pérez-Albela H. has mentioned, double underscores are usually used for "magic methods" like __init__(). In that usage, they lead and trail the method name. A single underscore in front of a method (or variable) name, like _index, usually means "this is a private member variable or a private method, and should not be used by other code". This is simply convention and does not have any special meaning to the Python interpreter, whereas some double-underscore methods do have special meaning.

Now, as for the question you asked: __ (a double underscore) as a variable name is not a normal thing in Python coding. The more usual convention is to use _ (a single underscore) for any variable whose contents you don't need. See What is the purpose of the single underscore "_" variable in Python? for more details.

However, there are sometimes reasons why a programmer might want to avoid using _ as a variable name. Specifically, if he's running code in an interactive Python interpreter session. In an interactive session, the value returned from the previous expression is stored in a variable named _. So if you're used to using _ as "a variable whose contents I don't care about" but you're in an interactive Python interpreter session, you might switch to using __ for that purpose instead.

So my best guess is that the sample code you've seen was intended to be run in an interactive Python session, where _ already has a special meaning. And so the person writing the code used __ for his throwaway variables, instead of _ which would be more commonly used.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: class with double underscore

From Dev

Understanding memory usage in python

From Dev

Other builtin or practical examples of python `with` statement usage?

From Dev

Replacing single underscore with double underscore

From Dev

Need help understanding *args + reduce usage in Python

From Dev

Understanding underscore bind

From Dev

Understanding Underscore's _.each

From Dev

How recommended is using custom double underscore variables in Python?

From Dev

Scala underscore usage in lambdas

From Dev

Practical usage for linked data

From Dev

Practical usage of params indexer

From Dev

Practical usage of Hadoop in project

From Dev

Practical usage of XML?

From Dev

Double underscore in front of a variable

From Dev

Is there a name for double underscore functions?

From Dev

Use of double underscore in C

From Dev

Double underscore vs single underscore in nodeJS

From Dev

Underscore usage in Scala's identifier

From Dev

Usage of underscore as argument of a macro function

From Dev

Basic python IO, please melp understanding file directory usage.

From Dev

Practical examples of OWIN middleware usage

From Dev

Practical usage of AutoMapper with POST request

From Dev

Practical usage of AutoMapper with POST request

From Dev

practical usage of /etc/networks file

From Dev

Disable double underscore behaviour for Sequel

From Dev

what is the meaning of double underscore in php?

From Dev

Double underscore to assignment - Sublime and R

From Dev

What does the Python naming convention with single/double underscore before a function mean?

From Java

Understanding Spring @Autowired usage