mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +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
|
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
|
.. 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.
|
completed and ``i`` is left with its final value of 4.
|
||||||
|
|
||||||
What's particularly nasty about this gotcha is the seemingly prevalent
|
What's particularly nasty about this gotcha is the seemingly prevalent
|
||||||
misinformation that this has something to do with ``lambda``\s in Python.
|
misinformation that this has something to do with :ref:`lambdas <python:lambda>`
|
||||||
Functions created with a ``lambda`` expression are in no way special, and in
|
in Python. Functions created with a ``lambda`` expression are in no way special,
|
||||||
fact the same exact behavior is exhibited by just using an ordinary ``def``:
|
and in fact the same exact behavior is exhibited by just using an ordinary ``def``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. 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
|
Mutable types are those that allow in-place modification
|
||||||
of the content. Typical mutables are lists and dictionaries:
|
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.
|
can be modified in place. The same goes for dictionaries.
|
||||||
|
|
||||||
Immutable types provide no method for changing their content.
|
Immutable types provide no method for changing their content.
|
||||||
@@ -464,10 +464,11 @@ should be your preferred method.
|
|||||||
foo = ''.join([foo, 'ooo'])
|
foo = ''.join([foo, 'ooo'])
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
You can also use the ``%`` formatting operator to concatenate the
|
You can also use the :ref:`% <python:string-formatting>` formatting operator
|
||||||
pre-determined number of strings besides ``join()`` and ``+``. However,
|
to concatenate a pre-determined number of strings besides :py:meth:`str.join`
|
||||||
according to :pep:`3101`, the ``%`` operator became deprecated in
|
and ``+``. However, according to :pep:`3101`, the ``%`` operator became
|
||||||
Python 3.1 and will be replaced by the ``format()`` method in the later versions.
|
deprecated in Python 3.1 and will be replaced by the :py:meth:`str.format`
|
||||||
|
method in the later versions.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|||||||
+10
-13
@@ -335,7 +335,7 @@ Instead, use a list comprehension:
|
|||||||
four_lists = [[] for __ in xrange(4)]
|
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']
|
letters = ['s', 'p', 'a', 'm']
|
||||||
word = ''.join(letters)
|
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
|
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
|
<http://docs.python.org/library/stdtypes.html#truth-value-testing>`_ for a
|
||||||
list of what is considered false.
|
list of what is considered false.
|
||||||
|
|
||||||
@@ -466,8 +466,8 @@ list of what is considered false.
|
|||||||
Access a Dictionary Element
|
Access a Dictionary Element
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Don't use the ``has_key`` function. Instead use ``x in d`` syntax, or pass
|
Don't use the :py:meth:`dict.has_key` method. Instead, use ``x in d`` syntax,
|
||||||
a default argument to ``get``.
|
or pass a default argument to :py:meth:`dict.get`.
|
||||||
|
|
||||||
**Bad**:
|
**Bad**:
|
||||||
|
|
||||||
@@ -497,10 +497,9 @@ Short Ways to Manipulate Lists
|
|||||||
|
|
||||||
`List comprehensions
|
`List comprehensions
|
||||||
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
|
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
|
||||||
provide a powerful, concise way to work with lists. Also, the `map
|
provide a powerful, concise way to work with lists. Also, the :py:func:`map`
|
||||||
<http://docs.python.org/library/functions.html#map>`_ and `filter
|
:py:func:`filter` functions can perform operations on lists using a different,
|
||||||
<http://docs.python.org/library/functions.html#filter>`_ functions can perform
|
more concise syntax.
|
||||||
operations on lists using a different concise syntax.
|
|
||||||
|
|
||||||
**Bad**:
|
**Bad**:
|
||||||
|
|
||||||
@@ -540,8 +539,7 @@ operations on lists using a different concise syntax.
|
|||||||
# Or:
|
# Or:
|
||||||
a = map(lambda i: i + 3, a)
|
a = map(lambda i: i + 3, a)
|
||||||
|
|
||||||
Use `enumerate <http://docs.python.org/library/functions.html#enumerate>`_ to
|
Use :py:func:`enumerate` keep a count of your place in the list.
|
||||||
keep a count of your place in the list.
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@@ -552,9 +550,8 @@ keep a count of your place in the list.
|
|||||||
# 1 4
|
# 1 4
|
||||||
# 2 5
|
# 2 5
|
||||||
|
|
||||||
The ``enumerate`` function has better readability than handling a counter
|
The :py:func:`enumerate` function has better readability than handling a
|
||||||
manually. Moreover,
|
counter manually. Moreover, it is better optimized for iterators.
|
||||||
it is better optimized for iterators.
|
|
||||||
|
|
||||||
Read From a File
|
Read From a File
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|||||||
Reference in New Issue
Block a user