mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Recommend the use of double underscore throwaway variables
This commit is contained in:
+10
-6
@@ -236,19 +236,23 @@ Create an ignored variable
|
|||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
If you need to assign something (for instance, in :ref:`unpacking-ref`) but
|
If you need to assign something (for instance, in :ref:`unpacking-ref`) but
|
||||||
will not need that variable, use ``_``:
|
will not need that variable, use ``__``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
filename = 'foobar.txt'
|
filename = 'foobar.txt'
|
||||||
basename, _, ext = filename.rpartition()
|
basename, __, ext = filename.rpartition()
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
"``_``" is commonly used as an alias for the :func:`~gettext.gettext`
|
Many Python style guides recommend the use of a single underscore "``_``"
|
||||||
function. If your application uses (or may someday use) :mod:`gettext`,
|
for throwaway variables rather than the double underscore "``__``"
|
||||||
you may want to avoid using ``_`` for ignored variables, as you may
|
recommended here. The issue is that "``_``" is commonly used as an alias
|
||||||
accidentally shadow :func:`~gettext.gettext`.
|
for the :func:`~gettext.gettext` function, and is also used at the
|
||||||
|
interactive prompt to hold the value of the last operation. Using a
|
||||||
|
double underscore instead is just as clear and almost as convenient,
|
||||||
|
and eliminates the risk of accidentally interfering with either of
|
||||||
|
these other use cases.
|
||||||
|
|
||||||
Create a length-N list of the same thing
|
Create a length-N list of the same thing
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|||||||
Reference in New Issue
Block a user