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
<bool>=any(<collection>) # Is `bool(<el>)` True for any el?
778
-
<bool>=all(<collection>) # True for all? Also True if empty.
777
+
<bool>=any(<collection>) # Is bool(<el>) True for any el?
778
+
<bool>=all(<collection>) # Is it true for all (or empty)?
779
779
```
780
780
781
781
### Conditional Expression
782
782
```python
783
-
<obj>=<exp>if<condition>else<exp># Only one expression is evaluated.
783
+
<obj>=<exp>if<condition>else<exp># Evaluates only one expression.
784
784
```
785
785
786
786
```python
787
-
>>> [i if i else'zero'for i in (0, 1, 2, 3)] # `any([0, '', [], None]) == False`.
787
+
>>> [i if i else'zero'for i in (0, 1, 2, 3)] # `any(['', [], None])` is False.
788
788
['zero', 1, 2, 3]
789
789
```
790
790
791
791
### And, Or
792
792
```python
793
-
<obj>=<exp>and<exp> [and...] # Returns first false or last obj.
794
-
<obj>=<exp>or<exp> [or...] # Returns first true or last obj.
793
+
<obj>=<exp>and<exp> [and...] # Returns first false or last obj.
794
+
<obj>=<exp>or<exp> [or...] # Returns first true or last obj.
795
795
```
796
796
797
797
### Walrus Operator
798
798
```python
799
-
>>> [i for ch in'0123'if (i :=int(ch)) >0] # Assigns to var in mid-sentence.
799
+
>>> [i for ch in'0123'if (i :=int(ch)) >0] # Assigns to var in mid-sentence.
800
800
[1, 2, 3]
801
801
```
802
802
803
803
### Named Tuple, Enum, Dataclass
804
804
```python
805
805
from collections import namedtuple
806
-
Point = namedtuple('Point', 'x y') # Creates tuple's subclass.
807
-
point = Point(0, 0) # Returns its instance.
806
+
Point = namedtuple('Point', 'x y') # Creates tuple's subclass.
807
+
point = Point(0, 0) # Returns its instance.
808
808
809
809
from enum import Enum
810
-
Direction = Enum('Direction', 'N E S W') # Creates an enumeration.
811
-
direction = Direction.N # Returns its member.
810
+
Direction = Enum('Direction', 'N E S W') # Creates an enumeration.
811
+
direction = Direction.N # Returns its member.
812
812
813
813
from dataclasses import make_dataclass
814
-
Player = make_dataclass('Player', ['loc', 'dir']) # Creates a normal class.
815
-
player = Player(point, direction) # Returns its instance.
814
+
Player = make_dataclass('Player', ['loc', 'dir']) # Creates a normal class.
815
+
player = Player(point, direction) # Returns its instance.
816
816
```
817
817
818
818
@@ -978,18 +978,18 @@ class MyClass:
978
978
979
979
#### Expressions that call object's str() special method:
980
980
```python
981
-
print(obj)
982
981
f'{obj}'
982
+
print(obj)
983
983
logging.warning(obj)
984
-
csv.writer(<file>).writerow([obj])
984
+
<csv_writer>.writerow([obj])
985
985
```
986
986
987
987
#### Expressions that call object's repr() special method:
988
988
```python
989
+
f'{obj!r}'
989
990
print/str/repr([obj])
990
991
print/str/repr({obj: obj})
991
-
f'{obj!r}'
992
-
Z = make_dataclass('Z', ['a']); print/str/repr(Z(obj))
992
+
print/str/repr(<dataclass>(obj))
993
993
```
994
994
995
995
### Subclass
@@ -1289,7 +1289,7 @@ class MySequence:
1289
1289
### ABC Sequence
1290
1290
***It's a richer interface than the basic sequence that also requires just getitem() and len().**
1291
1291
***Extending it generates iter(), contains(), reversed(), index() and count() special methods.**
1292
-
***Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, abc.Sequence)'` would return False even if MySequence had all the methods defined. It however recognizes list, tuple, range, str, bytes, bytearray, array, memoryview and deque, since they are registered as Sequence's virtual subclasses.**
1292
+
***Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why exp. `'issubclass(MySequence, abc.Sequence)'` would return False even if MySequence had all the methods defined. It however recognizes list, tuple, range, str, bytes, bytearray, array, memoryview and deque, since they are registered as Sequence's virtual subclasses.**
0 commit comments