Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-05 12:58:47 +01:00 committed by GitHub
parent 11bdf5014b
commit e13911443c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -449,9 +449,83 @@ Constants are usually defined on a module level and written in all capital lette
Always decide whether a class's methods and instance variables (collectively: "attributes") should be public or non-public. If in doubt, choose non-public; it's easier to make it public later than to make a public attribute non-public. […] Public attributes should have no leading underscores.
[+ many rules we --- hopefully -- might not need in our daily use… I will spare you those.]
## [Programming Recommendations](https://peps.python.org/pep-0008/#programming-recommendations)
*Comparisons to singletons like None should always be done with is or is not, never the equality operators.
*Use is not operator rather than not ... is. While both expressions are functionally identical, the former is more readable and preferred:
```python
# Correct:
if foo is not None:
```
* Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier:
```python
# Correct:
def f(x): return 2*x
# Wrong:
f = lambda x: 2*x
```
* Derive exceptions from Exception rather than BaseException.
* When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause:
```python
try:
import platform_specific_module
except ImportError:
platform_specific_module = None
```
* A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:).
* Additionally, for all try/except clauses, limit the try clause to the absolute minimum amount of code necessary. Again, this avoids masking bugs:
```python
# Correct:
try:
value = collection[key]
except KeyError:
return key_not_found(key)
else:
return handle_value(value)
```
* When a resource is local to a particular section of code, use a with statement to ensure it is cleaned up promptly and reliably after use. A try/finally statement is also acceptable.
* Context managers should be invoked through separate functions or methods whenever they do something other than acquire and release resources:
```python
# Correct:
with conn.begin_transaction():
do_stuff_in_transaction(conn)
```
* Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable):
```python
# Correct:
def foo(x):
if x >= 0:
return math.sqrt(x)
else:
return None
def bar(x):
if x < 0:
return None
return math.sqrt(x)
```
* Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.
startswith() and endswith() are cleaner and less error prone:
```python
# Correct:
if foo.startswith('bar'):
# Wrong:
if foo[:3] == 'bar':
```