Skip to content

Commit e58a53c

Browse files
committed
📝 Add recommendations on programming style
* Add CSS classes 'good' and 'bad'
1 parent 9455453 commit e58a53c

8 files changed

Lines changed: 168 additions & 27 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ emergencies when we need to start branches for older versions.
1919
Added
2020
~~~~~
2121

22+
* 📝 Add recommendations on programming style
23+
24+
* Add CSS classes 'good' and 'bad'
25+
2226
* 📝 Add what’s new in Python 3.15
2327

2428
* Add lacy imports

‎docs/_static/css/cusy.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ dl.simple dt,
1010
dl:not([class]) dt {
1111
font-weight: 700;
1212
}
13+
14+
.good pre {
15+
border-left: 3px solid rgba(74, 182, 93, 1);
16+
}
17+
18+
.bad pre {
19+
border-left: 3px solid rgb(244, 76, 78);
20+
}

‎docs/control-flow/boolean.rst‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,22 @@ considered ``True``.
9595
>>> x is not None
9696
True
9797
98-
The Python style guide in :pep:`8` also recommends that you should check for
99-
identity with :doc:`../types/none` and not for values, so never use ``x ==
100-
None``, but always use ``x is None`` instead.
98+
.. tip::
99+
The Python Style Guide in :pep:`PEP 8 <8#programming-recommendations>`
100+
recommends that you check for equality with :doc:`../types/none` rather
101+
than for values; in other words, not
102+
103+
.. code-block:: pycon
104+
:class: bad
105+
106+
>>> x == None
107+
108+
but
109+
110+
.. code-block:: pycon
111+
:class: good
112+
113+
>>> x is None
101114
102115
``and``, ``not``, ``or``
103116
are logical operators that we can use to link the above checks:

‎docs/control-flow/conditional.rst‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ Line 8
3939
The optional ``else`` clause denotes a code block that is only executed if
4040
the other conditional blocks, ``if`` and ``elif``, are all false.
4141

42+
.. tip::
43+
Do not compare Boolean values with ``True`` or ``False`` using ``==`` or
44+
``is``:
45+
46+
.. code-block:: pycon
47+
:class: bad
48+
49+
>>> if x < 1 == True:
50+
... pass
51+
...
52+
>>> if x < 1 is True:
53+
... pass
54+
...
55+
4256
Furthermore, the conditions can be linked logically, for example:
4357

4458
.. code-block:: pycon

‎docs/control-flow/exceptions.rst‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,47 @@ Line 19
9898
only handled when they occur. This Pythonic approach to error handling is
9999
often described as :term:`EAFP`.
100100

101+
.. tip::
102+
When handling exceptions, you should specify particular exceptions rather
103+
than simply using the ``except`` clause, as a simple ``except`` clause also
104+
catches the ``SystemExit`` and ``KeyboardInterrupt`` exceptions, making it
105+
difficult to terminate a programme using :kbd:`Ctrl+C` and potentially
106+
masking other problems.
107+
108+
If you wish to catch all exceptions indicating programme errors, use
109+
``except Exception:``.
110+
111+
.. tip::
112+
In most cases, simple ``except`` clauses should be limited to two scenarios:
113+
if the exception handler outputs or logs a traceback, the user will at least
114+
be aware that an error has occurred.
115+
116+
.. tip::
117+
In all ``try`` ``except`` statements, the ``try`` statement should be limited
118+
to the absolute minimum amount of code necessary. The following ``try``
119+
statement is too broad, and the ``except`` statement also catches the
120+
``KeyError``:
121+
122+
.. code-block:: python
123+
:class: bad
124+
125+
try:
126+
return process_value(collection[key])
127+
except KeyError:
128+
return key_not_found(key)
129+
130+
Better is:
131+
132+
.. code-block:: python
133+
:class: good
134+
135+
try:
136+
value = collection[key]
137+
except KeyError:
138+
return key_not_found(key)
139+
else:
140+
return process_value(value)
141+
101142
Checks
102143
------
103144

‎docs/functions/index.rst‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The basic syntax for a Python function definition is
99
.. code-block:: python
1010
1111
def function_name(param1, param2):
12-
body
12+
pass
1313
1414
As with :doc:`control streams </control-flow/index>`, Python uses indentation to
1515
separate the function from the function definition. The following simple example
@@ -19,13 +19,13 @@ inserts the code into a function so that you can call it to get the `factorial
1919
.. code-block:: python
2020
:linenos:
2121
22-
def fact(n):
23-
"""Return the factorial of the given number."""
24-
f = 1
25-
while n > 0:
26-
f = f * n
27-
n = n - 1
28-
return f
22+
def fact(n):
23+
"""Return the factorial of the given number."""
24+
f = 1
25+
while n > 0:
26+
f = f * n
27+
n = n - 1
28+
return f
2929
3030
Line 2
3131
This is an optional documentation string, or ``docstring``. You can get its
@@ -40,7 +40,8 @@ Line 2
4040
information.
4141

4242
.. seealso::
43-
* :ref:`napoleon`
43+
* :doc:`../document/sphinx/docstrings`
44+
* :pep:`257`
4445

4546
Line 7
4647
The value is returned after the function is called. You can also write

‎docs/functions/lambda.rst‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ is declared without a name. It is a small and restricted function that is no
66
longer than one line. Like a normal function, a lambda function can have several
77
arguments, but only one expression that is evaluated and returned.
88

9-
The syntax of a lambda function is
10-
11-
:samp:`lambda {ARGUMENTS}: {EXPRESSION}`
12-
13-
.. code-block:: pycon
14-
15-
>>> add = lambda x, y: x + y
16-
>>> add(2, 3)
17-
5
9+
The syntax of a lambda function is :samp:`lambda {ARGUMENTS}: {EXPRESSION}`
1810

1911
.. note::
2012
There is no ``return`` statement in the lambda function. The single
2113
expression after the colon is the return value.
2214

23-
In the next example, a lambda function is created within a function call.
24-
However, there is no global variable to store the values of the lambda function:
15+
.. tip::
16+
However, always use a ``def`` statement instead of an assignment that binds a
17+
lambda expression directly to an identifier – in other words, use
18+
19+
.. code-block:: pycon
20+
:class: good
21+
22+
>>> def add(x, y):
23+
... return x + y
24+
...
25+
>>> add(2, 3)
26+
5
27+
28+
rather than
29+
30+
.. code-block:: pycon
31+
:class: bad
32+
33+
>>> add = lambda x, y: x + y
34+
>>> add(2, 3)
35+
5
36+
37+
This is generally more useful for tracebacks and string representations. The
38+
only advantage of lambda functions is that they can be embedded within a larger
39+
expression. In the next example, we’ll see how a lambda function is created
40+
within a function call. In this case, there is no global variable to store the
41+
values of the lambda function:
2542

2643
.. code-block:: pycon
2744
:linenos:

‎docs/types/none.rst‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,36 @@ For example, we can check whether :doc:`../types/strings/index` are empty in an
4141
...
4242
No value was specified.
4343
44+
.. tip::
45+
With sequences such as :doc:`strings/index`, :doc:`sequences-sets/lists` and
46+
:doc:`sequences-sets/tuples`, you can make use of the fact that empty
47+
sequences are ``False``:
48+
49+
.. code-block:: pycon
50+
:class: good
51+
52+
>>> if seq:
53+
... pass
54+
...
55+
56+
You don’t need to go around using :func:`len` for this:
57+
58+
.. code-block:: pycon
59+
:class: bad
60+
61+
>>> if len(seq):
62+
... pass
63+
...
64+
4465
:class:`None` stands for emptiness
4566
----------------------------------
4667

4768
.. code-block:: pycon
4869
4970
>>> titles = {7.0: "Data Types", 7.1: "Lists", 7.2: "Tuples"}
5071
>>> third_title = titles.get("7.3")
51-
>>> print(third_title)
52-
None
72+
>>> type(third_title)
73+
<class 'NoneType'>
5374
5475
The default return value of a function is :class:`None`
5576
-------------------------------------------------------
@@ -62,5 +83,27 @@ return a value, which means that it returns ``None`` by default:
6283
>>> def myfunc():
6384
... pass
6485
...
65-
>>> print(myfunc())
66-
None
86+
>>> type(myfunc())
87+
<class 'NoneType'>
88+
89+
However, ``return None`` is more meaningful and is also recommended by :pep:`PEP
90+
8 <8#programming-recommendations>`, so don’t write
91+
92+
.. code-block::
93+
:class: bad
94+
95+
def myfunc(x):
96+
if x >= 0:
97+
return x**2
98+
99+
but
100+
101+
.. code-block::
102+
:class: good
103+
:emphasize-lines: 4-
104+
105+
def myfunc(x):
106+
if x >= 0:
107+
return x**2
108+
else:
109+
return None

0 commit comments

Comments
 (0)