Skip to content

Commit 911d829

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#119 from guibog/master
Adding a reference section with a new book and line continuation style guide.
2 parents 7c64e88 + 2d48df0 commit 911d829

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

docs/intro/learning.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ chapters detail best practices with writing documentation, test-driven
9494
development, version control, and optimization/profiling.
9595

9696
`Expert Python Programming <http://www.packtpub.com/expert-python-programming/book>`_
97+
98+
99+
References
100+
----------
101+
102+
Python in a Nutshell
103+
~~~~~~~~~~~~~~~~~~~~
104+
105+
Python in a Nutshell, written by Alex Martelli, covers most cross-platform python's usage,
106+
from its syntax to built-in libraries to advanced topics such as writing C extensions.

docs/starting/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If so, you do not need to install or configure anything else to use Python. Havi
88
Installation Guides
99
-------------------
1010

11-
These guides go over the proper installation of :ref:`Python 2.7 <which-python>` for development purproses, as well as distribute, pip, and virtualenv setup.
11+
These guides go over the proper installation of :ref:`Python 2.7 <which-python>` for development purposes, as well as distribute, pip, and virtualenv setup.
1212

1313
- :ref:`Mac OS X <install-osx>`.
1414
- :ref:`Microsoft Windows<install-windows>`.

docs/writing/structure.rst

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,56 @@ Structuring your project properly is extremely important.
88
Structure is Key
99
----------------
1010

11+
Thanks to the way imports and module are handled in Python, it is
12+
relatively easy to structure a python project. Easy, here, means
13+
actually that you have not many constraints and that the module
14+
importing model is easy grasp. Therefore, you are left with the
15+
pure architectural task of drawing the different parts of your
16+
project and their interactions.
17+
18+
Easy structuration of a project means it is also easy
19+
to do it poorly. Some signs of a poorly structured projects
20+
include:
21+
22+
- Multiple and messy circular dependencies: if your classes
23+
Table and Chair in furn.py need to import Carpenter from workers.py
24+
to answer to a question such as table.isdoneby(),
25+
and if convertly the class Carpenter need to import Table and Chair,
26+
for example to answer to carpenter.whatdo(), then you
27+
have a circular dependency, and will have to resort to
28+
fragile hacks such has using import statements inside
29+
methods or functions.
30+
31+
- Hidden coupling. Each and every change in Table implementation
32+
breaks 20 tests in unrelated test cases because it breaks Carpenter's code,
33+
which requires very careful surgery to adapt the change. This means
34+
you have too many assumptions about Table in Carpenter's code or the
35+
reverse.
36+
37+
- Heavy usage of global state or context: Instead of explicitely
38+
passing ``(height, width, type, wood)`` to each other, Table
39+
and Carpenter rely on global variables that can be modified
40+
and are modified on the fly by different agent. You need to
41+
scrutinize all access to this global variables to understand why
42+
a rectangular table became a sqaure, and discover that a remote
43+
template code is also modifying this context, messing with
44+
table dimensions.
45+
46+
- Spaghetti code: Multiple pages of nested if clauses and for loops
47+
with a lot of copy-pasted procedural code and no
48+
proper segmentation are known as spaghetti code. Python's
49+
meaningful indentation (one of its most controversial feature) make
50+
it very hard to maintain this kind of code. So the good news is that
51+
you might not see too much of it.
52+
53+
- Ravioli code is more likely in Python: it consists of hundreds of
54+
similar little pieces of logic, often classes or objects, without
55+
proper structure. If you never can remember if you have to use
56+
FurnitureTable, AssetTable or Table, or even TableNew for your
57+
task at hand, you might be swimming in ravioli code.
58+
59+
60+
1161

1262

1363
Vendorizing Dependencies
@@ -20,4 +70,4 @@ Runners
2070

2171

2272
Further Reading
23-
---------------
73+
---------------

docs/writing/style.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,40 @@ values in before you return
317317
318318
square, cube = math_func(3)
319319
320+
Line Continuations
321+
~~~~~~~~~~~~~~~~~~
322+
323+
When a logical line of code is longer than the accepted limit, you need to split it over multiple
324+
physical lines. Python interpreter will join consecutive lines if the last character of the line is
325+
a backslash. This is helpful sometime but is preferably avoided, because of its fragility: a white
326+
space added to the end of the line, after the backslash, will break the code and may have unexpected
327+
results.
328+
329+
A prefered solution is to use parenthesis around your elements. Left with an unclosed parenthesis on an end-of-line
330+
the Python interpreter will join the next line until the parenthesis is closed. The same behavior holds for
331+
curly and square braces.
332+
333+
**Bad**:
334+
335+
.. code-block:: python
336+
337+
my_very_big_string = """For a long time I used to go to bed early. Sometimes, \
338+
when I had put out my candle, my eyes would close so quickly that I had not even \
339+
time to say “I’m going to sleep.”"""
340+
341+
from some.deep.module.inside.a.module import a_nice_function, another_nice_function, \
342+
yet_another_nice_functio
343+
344+
**Good**:
345+
346+
.. code-block:: python
347+
348+
my_very_big_string = ("For a long time I used to go to bed early. Sometimes, "
349+
"when I had put out my candle, my eyes would close so quickly that I had not even "
350+
time to say “I’m going to sleep.”")
351+
352+
from some.deep.module.inside.a.module import (a_nice_function, another_nice_function,
353+
yet_another_nice_functio)
354+
355+
However, more often than not having to split long logical line is a sign that you
356+
are trying to do too many things at the same time, which may hinder readability.

0 commit comments

Comments
 (0)