You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-31Lines changed: 30 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -640,9 +640,9 @@ import zoneinfo, dateutil.tz
640
640
641
641
### Decode
642
642
```python
643
-
<str>=<D/T/DT>.isoformat(sep='T') # Also `timespec='auto/hours/minutes/seconds/…'`.
643
+
<str>=<D/T/DT>.isoformat(sep='T') # Also `timespec='auto/hours/minutes/seconds…'`.
644
644
<str>=<D/T/DT>.strftime('<format>') # Custom string representation of the object.
645
-
<int>=<D/DT>.toordinal() # Days since NYE 1, ignoring time and timezone.
645
+
<int>=<D/DT>.toordinal() # Days since NYE 1. Ignores DT's time and zone.
646
646
<float>=<DTn>.timestamp() # Seconds since the Epoch, from local naive DT.
647
647
<float>=<DTa>.timestamp() # Seconds since the Epoch, from aware datetime.
648
648
```
@@ -794,7 +794,7 @@ from functools import reduce
794
794
795
795
### Walrus Operator
796
796
```python
797
-
>>> [i forain'0123'if (i :=int(a)) >0]# Assigns to variable mid-sentence.
797
+
>>> [i forchin'0123'if (i :=int(ch)) >0] # Assigns to variable mid-sentence.
798
798
[1, 2, 3]
799
799
```
800
800
@@ -885,8 +885,7 @@ def get_counter():
885
885
886
886
Decorator
887
887
---------
888
-
***A decorator takes a function, adds some functionality and returns it.**
889
-
***It can be any [callable](#callable), but is usually implemented as a function that returns a [closure](#closure).**
888
+
**A decorator takes a function, adds some functionality and returns it. It can be any [callable](#callable), but is usually implemented as a function that returns a [closure](#closure).**
890
889
891
890
```python
892
891
@decorator_name
@@ -1094,7 +1093,7 @@ Duck Types
1094
1093
**A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.**
1095
1094
1096
1095
### Comparable
1097
-
***If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`. That means all user-defined objects compare not equal by default, because id() returns object's unique identification number (its memory address).**
1096
+
***If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`. That means all user-defined objects compare not equal by default (because id() returns object's memory address that is unique during its lifetime).**
1098
1097
***Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.**
1099
1098
***Ne() automatically works on any object that has eq() defined.**
1100
1099
@@ -1199,8 +1198,8 @@ class Counter:
1199
1198
1200
1199
### Context Manager
1201
1200
***With statements only work on objects that have enter() and exit() special methods.**
1202
-
***Enter() should lock the resources and optionally return an object.**
1203
-
***Exit() should release the resources (for example close a file).**
1201
+
***Enter() should lock the resources and optionally return an object (file, lock, etc.).**
1202
+
***Exit() should release the resources (for example close a file, release a lock, etc.).**
1204
1203
***Any exception that happens inside the with block is passed to the exit() method.**
1205
1204
***The exit() method can suppress the exception by returning a true value.**
1206
1205
```python
@@ -1332,8 +1331,8 @@ from enum import Enum, auto
1332
1331
```python
1333
1332
class<enum_name>(Enum):
1334
1333
<member_name>= auto() # Increment of the last numeric value or 1.
1335
-
<member_name>=<value># Values don't have to be hashable.
1336
-
<member_name>=<el_1>, <el_2># Values can be collections (this is a tuple).
1334
+
<member_name>=<value># Values don't have to be hashable or unique.
1335
+
<member_name>=<el_1>, <el_2># Values can be collections, like this tuple.
1337
1336
```
1338
1337
***Methods receive the member they were called on as the 'self' argument.**
1339
1338
***Accessing a member named after a reserved keyword causes SyntaxError.**
@@ -1342,20 +1341,20 @@ class <enum_name>(Enum):
1342
1341
<member>=<enum>.<member_name># Returns a member. Raises AttributeError.
1343
1342
<member>=<enum>['<member_name>'] # Returns a member. Raises KeyError.
1344
1343
<member>=<enum>(<value>) # Returns a member. Raises ValueError.
1345
-
<str>=<member>.name # Returns member's name.
1346
-
<obj>=<member>.value # Returns member's value.
1344
+
<str>=<member>.name # Returns the member's name.
1345
+
<obj>=<member>.value # Returns the member's value.
1347
1346
```
1348
1347
1349
1348
```python
1350
-
<list>=list(<enum>) # Returns enum's members.
1351
-
<list>=[a.name for a in<enum>]# Returns enum's member names.
1352
-
<list>= [a.value forain<enum>] # Returns enum's member values.
1349
+
<list>=list(<enum>) # Returns a list of enum's members.
1350
+
<list>=<enum>._member_names_ # Returns a list of member names.
1351
+
<list>= [m.value formin<enum>] # Returns a list of member values.
1353
1352
```
1354
1353
1355
1354
```python
1356
-
<enum>=type(<member>) # Returns member's enum.
1357
-
<iter>= itertools.cycle(<enum>) # Returns endless iterator of members.
1358
-
<member>= random.choice(list(<enum>)) #Returns a random member.
1355
+
<enum>=type(<member>) # Returns an enum. Also <memb>.__class__.
1356
+
<iter>= itertools.cycle(<enum>) # Returns an endless iterator of members.
1357
+
<member>= random.choice(list(<enum>)) #Randomly selects one of the members.
1359
1358
```
1360
1359
1361
1360
### Inline
@@ -1397,8 +1396,8 @@ finally:
1397
1396
```
1398
1397
***Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
1399
1398
***Code inside the `'finally'` block will always be executed (unless a signal is received).**
1400
-
***All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only function block delimits scope).**
1401
-
***To catch signals use `'signal.signal(signal_number, <func>)'`.**
1399
+
***All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only a function block delimits scope).**
1400
+
***To catch signals use `'signal.signal(signal_number, handler_function)'`.**
1402
1401
1403
1402
### Catching Exceptions
1404
1403
```python
@@ -1407,10 +1406,10 @@ except <exception> as <name>: ...
1407
1406
except (<exception>, [...]): ...
1408
1407
except (<exception>, [...]) as<name>: ...
1409
1408
```
1410
-
***Also catches subclasses of the exception.**
1411
-
***Use `'traceback.print_exc()'` to print the full error message to stderr.**
1412
-
***Use `'print(<name>)'` to print just the cause of the exception (its arguments).**
1413
-
***Use `'logging.exception(<str>)'` to log the passed message, followed by the full error message of the caught exception. For details see [Logging](#logging).**
1409
+
***Also catches subclasses, e.g. `'IndexError'` is caught by `'except LookupError:'`.**
1410
+
***Use `'traceback.print_exc()'` to print the full error message to standard error stream.**
1411
+
***Use `'print(<name>)'` to print just the cause of the exception (i.e. its arguments).**
1412
+
***Use `'logging.exception(<str>)'` to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see [Logging](#logging).**
1414
1413
***Use `'sys.exc_info()'` to get exception type, object, and traceback of caught exception.**
0 commit comments