Is the Single Underscore "_" a Built-In Variable in Python

Is the single underscore _ a built-in variable in Python?

In the standard Python REPL, _ represents the last returned value -- at the point where you called len(_), _ was the value 'abc'.

For example:

>>> 10
10
>>> _
10
>>> _ + 5
15
>>> _ + 5
20

This is handled by sys.displayhook, and the _ variable goes in the builtins namespace with things like int and sum, which is why you couldn't find it in globals().

Note that there is no such functionality within Python scripts. In a script, _ has no special meaning and will not be automatically set to the value produced by the previous statement.

Also, beware of reassigning _ in the REPL if you want to use it like above!

>>> _ = "underscore"
>>> 10
10
>>> _ + 5

Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
_ + 5
TypeError: cannot concatenate 'str' and 'int' objects

This creates a global variable that hides the _ variable in the built-ins. To undo the assignment (and remove the _ from globals), you'll have to:

>>> del _

then the functionality will be back to normal (the builtins._ will be visible again).

What is the purpose of the single underscore _ variable in Python?

_ has 3 main conventional uses in Python:

  1. To hold the result of the last executed expression in an interactive
    interpreter session (see docs). This precedent was set by the standard CPython
    interpreter, and other interpreters have followed suit

  2. For translation lookup in i18n (see the
    gettext
    documentation for example), as in code like

    raise forms.ValidationError(_("Please enter a correct username"))
  3. As a general purpose "throwaway" variable name:

    1. To indicate that part
      of a function result is being deliberately ignored (Conceptually, it is being discarded.), as in code like:

      label, has_label, _ = text.partition(':')
    2. As part of a function definition (using either def or lambda), where
      the signature is fixed (e.g. by a callback or parent class API), but
      this particular function implementation doesn't need all of the
      parameters, as in code like:

      def callback(_):
      return True

      [For a long time this answer didn't list this use case, but it came up often enough, as noted here, to be worth listing explicitly.]

    This use case can conflict with the translation lookup use case, so it is necessary to avoid using _ as a throwaway variable in any code block that also uses it for i18n translation (many folks prefer a double-underscore, __, as their throwaway variable for exactly this reason).

    Linters often recognize this use case. For example year, month, day = date() will raise a lint warning if day is not used later in the code. The fix, if day is truly not needed, is to write year, month, _ = date(). Same with lambda functions, lambda arg: 1.0 creates a function requiring one argument but not using it, which will be caught by lint. The fix is to write lambda _: 1.0. An unused variable is often hiding a bug/typo (e.g. set day but use dya in the next line).

    The pattern matching feature added in Python 3.10 elevated this usage from "convention" to "language syntax" where match statements are concerned: in match cases, _ is a wildcard pattern, and the runtime doesn't even bind a value to the symbol in that case.

    For other use cases, remember that _ is still a valid variable name, and hence will still keep objects alive. In cases where this is undesirable (e.g. to release memory or external resources) an explicit del name call will both satisfy linters that the name is being used, and promptly clear the reference to the object.

What is the meaning of single and double underscore before an object name?

Single Underscore

In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way.
Using leading underscores for functions in a module indicates it should not be imported from somewhere else.

From the PEP-8 style guide:

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

Double Underscore (Name Mangling)

From the Python docs:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

And a warning from the same page:

Name mangling is intended to give classes an easy way to define “private” instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private.

Example

>>> class MyClass():
... def __init__(self):
... self.__superprivate = "Hello"
... self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

Why do some functions have underscores __ before and after the function name?

From the Python PEP 8 -- Style Guide for Python Code:

Descriptive: Naming Styles


The following special forms using leading or trailing underscores are
recognized (these can generally be combined with any case convention):

  • _single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

  • single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

    Tkinter.Toplevel(master, class_='ClassName')

  • __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).

  • __double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__,
    __import__ or __file__. Never invent such names; only use them as documented.

Note that names with double leading and trailing underscores are essentially reserved for Python itself: "Never invent such names; only use them as documented".

Assigning a value to single underscore _ in Python/IPython interpreter

The Python interpreter assigns the last expression value to _.

This behaviour is limited to the REPL interpreter only, and is intended to assist in interactive coding sessions:

>>> import math
>>> math.pow(3.0, 5)
243.0
>>> result = _
>>> result
243.0

The standard Python interpreter goes to some length to not trample on user-defined values though; if you yourself assign something else to _ then the interpreter will not overwrite that (technically speaking, the _ variable is a __builtin__ attribute, your own assignments are 'regular' globals). You are not using the standard Python interpreter though; you are using IPython, and that interpreter is not that careful.

IPython documents this behaviour explicitly:

The following GLOBAL variables always exist (so don’t overwrite them!):

  • [_] (a single underscore) : stores previous output, like Python’s default interpreter.

[...]

In the standard Python REPL environment, if you assigned something to _ you can still access the last expression result via __builtins__._ or by deleting the _ global that shadows it again (del _).

Outside of the Python interpreter, _ is by convention used as the name of the translatable text function (see the gettext module; external tools look for that function to extract translatable strings).

And, also by convention, using _ as an assignment target tells readers of your code that you are going to ignore that value; e.g. [random.random() for _ in range(5)] to generate a list of 5 random float values, or foo, bar, _ = three_values to signal a 3rd value from a tuple assignment will not be used. When _ is already used for a gettext function, __ can be used for the same purposes.

Why do we use _ in variable names?

It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:

class Foo
{
private int _counter;

public int GetCounter()
{
return _counter;
}

public int SetCounter(int counter)
{
_counter = counter;
}
}

What does __all__ mean in Python?

It's a list of public objects of that module, as interpreted by import *. It overrides the default of hiding everything that begins with an underscore.

Is _ (single underscore) a valid C++ variable name?

Yes, from The C++ Programming Language, 4th Edition:

A name (identifier) consists of a sequence of letters and digits. The
first character must be a letter. The underscore character, _, is
considered a letter.

What is the meaning of simply __ double underscore as a variable name? Just __ not follow another chars

According to the IPython docs, the _* values cache the values of recent outputs:

The following variables always exist:

  • _ (a single underscore): stores previous output, like Python’s default interpreter.
  • __ (two underscores): next previous.
  • ___ (three underscores): next-next previous.

Conversely, the _i* variable store recent inputs:

_i, _ii, _iii: store previous, next previous and next-next previous inputs.



Related Topics



Leave a reply



Submit