mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 14:50:19 +00:00
Let Sphinx link to Python documentation.
Instead of directly linking to the relevant passages.
This commit is contained in:
@@ -65,7 +65,7 @@ What You Should Do Instead
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Create a new object each time the function is called, by using a default arg to
|
||||
signal that no argument was provided (``None`` is often a good choice).
|
||||
signal that no argument was provided (:py:data:`None` is often a good choice).
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -137,9 +137,9 @@ is looked up in the surrounding scope at call time. By then, the loop has
|
||||
completed and ``i`` is left with its final value of 4.
|
||||
|
||||
What's particularly nasty about this gotcha is the seemingly prevalent
|
||||
misinformation that this has something to do with ``lambda``\s in Python.
|
||||
Functions created with a ``lambda`` expression are in no way special, and in
|
||||
fact the same exact behavior is exhibited by just using an ordinary ``def``:
|
||||
misinformation that this has something to do with :ref:`lambdas <python:lambda>`
|
||||
in Python. Functions created with a ``lambda`` expression are in no way special,
|
||||
and in fact the same exact behavior is exhibited by just using an ordinary ``def``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
@@ -383,7 +383,7 @@ Python has two kinds of built-in or user-defined types.
|
||||
|
||||
Mutable types are those that allow in-place modification
|
||||
of the content. Typical mutables are lists and dictionaries:
|
||||
All lists have mutating methods, like ``append()`` or ``pop()``, and
|
||||
All lists have mutating methods, like :py:meth:`list.append` or :py:meth:`list.pop`, and
|
||||
can be modified in place. The same goes for dictionaries.
|
||||
|
||||
Immutable types provide no method for changing their content.
|
||||
@@ -464,10 +464,11 @@ should be your preferred method.
|
||||
foo = ''.join([foo, 'ooo'])
|
||||
|
||||
.. note::
|
||||
You can also use the ``%`` formatting operator to concatenate the
|
||||
pre-determined number of strings besides ``join()`` and ``+``. However,
|
||||
according to :pep:`3101`, the ``%`` operator became deprecated in
|
||||
Python 3.1 and will be replaced by the ``format()`` method in the later versions.
|
||||
You can also use the :ref:`% <python:string-formatting>` formatting operator
|
||||
to concatenate a pre-determined number of strings besides :py:meth:`str.join`
|
||||
and ``+``. However, according to :pep:`3101`, the ``%`` operator became
|
||||
deprecated in Python 3.1 and will be replaced by the :py:meth:`str.format`
|
||||
method in the later versions.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
+10
-13
@@ -335,7 +335,7 @@ Instead, use a list comprehension:
|
||||
four_lists = [[] for __ in xrange(4)]
|
||||
|
||||
|
||||
A common idiom for creating strings is to use `join <http://docs.python.org/library/string.html#string.join>`_ on an empty string.::
|
||||
A common idiom for creating strings is to use :py:meth:`str.join` on an empty string.::
|
||||
|
||||
letters = ['s', 'p', 'a', 'm']
|
||||
word = ''.join(letters)
|
||||
@@ -433,7 +433,7 @@ Check if variable equals a constant
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
You don't need to explicitly compare a value to True, or None, or 0 - you can
|
||||
just add it to the if statement. See `Truth Value Testing
|
||||
just add it to the if statement. See :ref:`Truth Value Testing
|
||||
<http://docs.python.org/library/stdtypes.html#truth-value-testing>`_ for a
|
||||
list of what is considered false.
|
||||
|
||||
@@ -466,8 +466,8 @@ list of what is considered false.
|
||||
Access a Dictionary Element
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Don't use the ``has_key`` function. Instead use ``x in d`` syntax, or pass
|
||||
a default argument to ``get``.
|
||||
Don't use the :py:meth:`dict.has_key` method. Instead, use ``x in d`` syntax,
|
||||
or pass a default argument to :py:meth:`dict.get`.
|
||||
|
||||
**Bad**:
|
||||
|
||||
@@ -497,10 +497,9 @@ Short Ways to Manipulate Lists
|
||||
|
||||
`List comprehensions
|
||||
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
|
||||
provide a powerful, concise way to work with lists. Also, the `map
|
||||
<http://docs.python.org/library/functions.html#map>`_ and `filter
|
||||
<http://docs.python.org/library/functions.html#filter>`_ functions can perform
|
||||
operations on lists using a different concise syntax.
|
||||
provide a powerful, concise way to work with lists. Also, the :py:func:`map`
|
||||
:py:func:`filter` functions can perform operations on lists using a different,
|
||||
more concise syntax.
|
||||
|
||||
**Bad**:
|
||||
|
||||
@@ -540,8 +539,7 @@ operations on lists using a different concise syntax.
|
||||
# Or:
|
||||
a = map(lambda i: i + 3, a)
|
||||
|
||||
Use `enumerate <http://docs.python.org/library/functions.html#enumerate>`_ to
|
||||
keep a count of your place in the list.
|
||||
Use :py:func:`enumerate` keep a count of your place in the list.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -552,9 +550,8 @@ keep a count of your place in the list.
|
||||
# 1 4
|
||||
# 2 5
|
||||
|
||||
The ``enumerate`` function has better readability than handling a counter
|
||||
manually. Moreover,
|
||||
it is better optimized for iterators.
|
||||
The :py:func:`enumerate` function has better readability than handling a
|
||||
counter manually. Moreover, it is better optimized for iterators.
|
||||
|
||||
Read From a File
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
Reference in New Issue
Block a user