mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
copy edits
This commit is contained in:
@@ -266,10 +266,10 @@ logic (called pure functions) allow the following benefits:
|
||||
- Pure functions are easier to test with unit-tests: There is less
|
||||
need for complex context setup and data cleaning afterwards.
|
||||
|
||||
- Pure functions are easier to manipulate, decorate, and pass-around.
|
||||
- Pure functions are easier to manipulate, decorate, and pass around.
|
||||
|
||||
In summary, pure functions, without any context or side-effects, are more
|
||||
efficient building blocks than classes and objects for some architectures.
|
||||
In summary, pure functions are more efficient building blocks than classes
|
||||
and objects for some architectures because they have no context or side-effects.
|
||||
|
||||
Obviously, object-orientation is useful and even necessary in many cases, for
|
||||
example when developing graphical desktop applications or games, where the
|
||||
@@ -314,7 +314,7 @@ of the function logic.
|
||||
Dynamic typing
|
||||
--------------
|
||||
|
||||
Python is said to be dynamically typed, which means that variables
|
||||
Python is dynamically typed, which means that variables
|
||||
do not have a fixed type. In fact, in Python, variables are very
|
||||
different from what they are in many other languages, specifically
|
||||
statically-typed languages. Variables are not a segment of the computer's
|
||||
|
||||
@@ -88,7 +88,7 @@ Arguments can be passed to functions in four different ways.
|
||||
|
||||
1. **Positional arguments** are mandatory and have no default values. They are the
|
||||
simplest form of arguments and they can be used for the few function arguments
|
||||
that are fully part of the functions meaning and their order is natural. For
|
||||
that are fully part of the function's meaning and their order is natural. For
|
||||
instance, in ``send(message, recipient)`` or ``point(x, y)`` the user of the
|
||||
function has no difficulty remembering that those two functions require two
|
||||
arguments, and in which order.
|
||||
@@ -102,7 +102,7 @@ calls to ``send('Hello', 'World')`` and ``point(1, 2)``.
|
||||
2. **Keyword arguments** are not mandatory and have default values. They are often
|
||||
used for optional parameters sent to the function. When a function has more than
|
||||
two or three positional parameters, its signature is more difficult to remember
|
||||
and using keyword argument with default values is helpful. For instance, a more
|
||||
and using keyword arguments with default values is helpful. For instance, a more
|
||||
complete ``send`` function could be defined as ``send(message, to, cc=None, bcc=None)``.
|
||||
Here ``cc`` and ``bcc`` are optional, and evaluate to ``None`` when they are not
|
||||
passed another value.
|
||||
@@ -165,7 +165,7 @@ Avoid the magical wand
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
A powerful tool for hackers, Python comes with a very rich set of hooks and
|
||||
tools allowing to do almost any kind of tricky tricks. For instance, it is
|
||||
tools allowing you to do almost any kind of tricky tricks. For instance, it is
|
||||
possible to do each of the following:
|
||||
|
||||
* change how objects are created and instantiated
|
||||
@@ -212,7 +212,7 @@ Using this convention generously is encouraged: any method or property that is
|
||||
not intended to be used by client code should be prefixed with an underscore.
|
||||
This will guarantee a better separation of duties and easier modification of
|
||||
existing code; it will always be possible to publicize a private property,
|
||||
while privatising a public property might be a much harder operation.
|
||||
but making a public property private might be a much harder operation.
|
||||
|
||||
Returning values
|
||||
~~~~~~~~~~~~~~~~
|
||||
@@ -374,7 +374,7 @@ Even though both functions look identical, because *lookup_dict* is utilizing
|
||||
the fact that dictionaries in Python are hashtables, the lookup performance
|
||||
between the two is very different. Python will have to go through each item
|
||||
in the list to find a matching case, which is time consuming. By analysing
|
||||
the hash of the dictionary, finding keys in the dict can be done very quickly.
|
||||
the hash of the dictionary, finding keys in the dictionary can be done very quickly.
|
||||
For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_
|
||||
page.
|
||||
|
||||
@@ -422,7 +422,7 @@ Conforming your Python code to PEP 8 is generally a good idea and helps make
|
||||
code more consistent when working on projects with other developers. There
|
||||
is a command-line program, `pep8 <https://github.com/jcrocholl/pep8>`_,
|
||||
that can check your code for conformance. Install it by running the following
|
||||
command in your Terminal:
|
||||
command in your terminal:
|
||||
|
||||
|
||||
.. code-block:: console
|
||||
@@ -639,6 +639,6 @@ and square braces.
|
||||
from some.deep.module.inside.a.module import (
|
||||
a_nice_function, another_nice_function, yet_another_nice_function)
|
||||
|
||||
However, more often than not having to split long logical line is a sign that
|
||||
However, more often than not, having to split a long logical line is a sign that
|
||||
you are trying to do too many things at the same time, which may hinder
|
||||
readability.
|
||||
|
||||
@@ -19,8 +19,8 @@ Some general rules of testing:
|
||||
usually handled by :meth:`setUp()` and :meth:`tearDown()` methods.
|
||||
|
||||
- Try hard to make tests that run fast. If one single test needs more than a
|
||||
few millisecond to run, development will be slowed down or the tests will
|
||||
not be run as often as desirable. In some cases, tests can't be fast because
|
||||
few milliseconds to run, development will be slowed down or the tests will
|
||||
not be run as often as is desirable. In some cases, tests can't be fast because
|
||||
they need a complex data structure to work on, and this data structure must
|
||||
be loaded every time the test runs. Keep these heavier tests in a separate
|
||||
test suite that is run by some scheduled task, and run all other tests as
|
||||
|
||||
Reference in New Issue
Block a user