@@ -10,7 +10,7 @@ Contents
10
10
--------
11
11
**   ;  ;  ; ** ** 1. Collections:** **   ; ** ** [ ` List ` ] ( #list ) ** __ ,__ ** [ ` Dictionary ` ] ( #dictionary ) ** __ ,__ ** [ ` Set ` ] ( #set ) ** __ ,__ ** [ ` Tuple ` ] ( #tuple ) ** __ ,__ ** [ ` Range ` ] ( #range ) ** __ ,__ ** [ ` Enumerate ` ] ( #enumerate ) ** __ ,__ ** [ ` Iterator ` ] ( #iterator ) ** __ ,__ ** [ ` Generator ` ] ( #generator ) ** __ .__
12
12
**   ;  ;  ; ** ** 2. Types:** **   ;  ;  ;  ;  ;  ;  ;  ;  ;  ; ** ** [ ` Type ` ] ( #type ) ** __ ,__ ** [ ` String ` ] ( #string ) ** __ ,__ ** [ ` Regular_Exp ` ] ( #regex ) ** __ ,__ ** [ ` Format ` ] ( #format ) ** __ ,__ ** [ ` Numbers ` ] ( #numbers-1 ) ** __ ,__ ** [ ` Combinatorics ` ] ( #combinatorics ) ** __ ,__ ** [ ` Datetime ` ] ( #datetime ) ** __ .__
13
- **   ;  ;  ; ** ** 3. Syntax:** **   ;  ;  ;  ;  ;  ;  ;  ;  ; ** ** [ ` Args ` ] ( #arguments ) ** __ ,__ ** [ ` Inline ` ] ( #inline ) ** __ ,__ ** [ ` Import ` ] ( #imports ) ** __ ,__ ** [ ` Decorator ` ] ( #decorator ) ** __ ,__ ** [ ` Class ` ] ( #class ) ** __ ,__ ** [ ` Duck_Types ` ] ( #duck-types ) ** __ ,__ ** [ ` Enum ` ] ( #enum ) ** __ ,__ ** [ ` Exception ` ] ( #exceptions ) ** __ .__
13
+ **   ;  ;  ; ** ** 3. Syntax:** **   ;  ;  ;  ;  ;  ;  ;  ;  ; ** ** [ ` Function ` ] ( #function ) ** __ ,__ ** [ ` Inline ` ] ( #inline ) ** __ ,__ ** [ ` Import ` ] ( #imports ) ** __ ,__ ** [ ` Decorator ` ] ( #decorator ) ** __ ,__ ** [ ` Class ` ] ( #class ) ** __ ,__ ** [ ` Duck_Type ` ] ( #duck-types ) ** __ ,__ ** [ ` Enum ` ] ( #enum ) ** __ ,__ ** [ ` Except ` ] ( #exceptions ) ** __ .__
14
14
**   ;  ;  ; ** ** 4. System:** **   ;  ;  ;  ;  ;  ;  ;  ; ** ** [ ` Exit ` ] ( #exit ) ** __ ,__ ** [ ` Print ` ] ( #print ) ** __ ,__ ** [ ` Input ` ] ( #input ) ** __ ,__ ** [ ` Command_Line_Arguments ` ] ( #command-line-arguments ) ** __ ,__ ** [ ` Open ` ] ( #open ) ** __ ,__ ** [ ` Path ` ] ( #paths ) ** __ ,__ ** [ ` OS_Commands ` ] ( #os-commands ) ** __ .__
15
15
**   ;  ;  ; ** ** 5. Data:** **   ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ; ** ** [ ` JSON ` ] ( #json ) ** __ ,__ ** [ ` Pickle ` ] ( #pickle ) ** __ ,__ ** [ ` CSV ` ] ( #csv ) ** __ ,__ ** [ ` SQLite ` ] ( #sqlite ) ** __ ,__ ** [ ` Bytes ` ] ( #bytes ) ** __ ,__ ** [ ` Struct ` ] ( #struct ) ** __ ,__ ** [ ` Array ` ] ( #array ) ** __ ,__ ** [ ` Memory_View ` ] ( #memory-view ) ** __ ,__ ** [ ` Deque ` ] ( #deque ) ** __ .__
16
16
**   ;  ;  ; ** ** 6. Advanced:** **   ;  ;  ; ** ** [ ` Operator ` ] ( #operator ) ** __ ,__ ** [ ` Match_Stmt ` ] ( #match-statement ) ** __ ,__ ** [ ` Logging ` ] ( #logging ) ** __ ,__ ** [ ` Introspection ` ] ( #introspection ) ** __ ,__ ** [ ` Threading ` ] ( #threading ) ** __ ,__ ** [ ` Coroutines ` ] ( #coroutines ) ** __ .__
@@ -671,86 +671,72 @@ import zoneinfo, dateutil.tz
671
671
```
672
672
673
673
674
- Arguments
675
- ---------
676
- ### Inside Function Call
674
+ Function
675
+ --------
676
+ ** Independent block of code that returns a value when called. **
677
677
``` python
678
- func( < positional_args > ) # func(0, 0)
679
- func( < keyword_args > ) # func(x=0, y=0)
680
- func( < positional_args > , < keyword_args > ) # func(0 , y=0)
678
+ def < func_name > ( < nondefault_args > ): ... # E.g. `def func(x, y): ...`.
679
+ def < func_name > ( < default_args > ): ... # E.g. `def func(x=0, y=0): ...`.
680
+ def < func_name > ( < nondefault_args > , < default_args > ): ... # E.g. `def func(x , y=0): ...`.
681
681
```
682
+ * ** Function returns None if it doesn't encounter ` 'return <obj/exp>' ` statement.**
683
+ * ** Before modifying a global variable from within the function run ` 'global <var_name>' ` .**
684
+ * ** Default values are evaluated when function is first encountered in the scope. Any mutation of a mutable default value will persist between invocations!**
685
+
686
+ ### Function Call
682
687
683
- ### Inside Function Definition
684
688
``` python
685
- def func (<nondefault_args>): ... # def func(x, y): .. .
686
- def func (<default_args>): ... # def func(x=0, y=0): .. .
687
- def func (<nondefault_args >, <default_args>): ... # def func(x , y=0): .. .
689
+ < obj > = < function > ( < positional_args > ) # E.g. ` func(0, 0)` .
690
+ < obj > = < function > ( < keyword_args > ) # E.g. ` func(x=0, y=0)` .
691
+ < obj > = < function > ( < positional_args > , < keyword_args > ) # E.g. ` func(0 , y=0)` .
688
692
```
689
- * ** Default values are evaluated when function is first encountered in the scope.**
690
- * ** Any mutation of a mutable default value will persist between invocations!**
691
693
692
694
693
695
Splat Operator
694
696
--------------
695
- ### Inside Function Call
696
697
** Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.**
697
698
``` python
698
- args = (1 , 2 )
699
- kwargs = {' x' : 3 , ' y' : 4 , ' z' : 5 }
699
+ args, kwargs = (1 , 2 ), {' z' : 3 }
700
700
func(* args, ** kwargs)
701
701
```
702
702
703
703
#### Is the same as:
704
704
``` python
705
- func(1 , 2 , x = 3 , y = 4 , z = 5 )
705
+ func(1 , 2 , z = 3 )
706
706
```
707
707
708
708
### Inside Function Definition
709
709
** Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.**
710
710
``` python
711
- def add (* a ):
712
- return sum (a)
713
- ```
714
-
715
- ``` python
711
+ >> > def add (* a ):
712
+ ... return sum (a)
713
+ ...
716
714
>> > add(1 , 2 , 3 )
717
715
6
718
716
```
719
717
720
- #### Legal argument combinations:
721
- ``` python
722
- def f (* args ): ... # f(1, 2, 3)
723
- def f (x , * args ): ... # f(1, 2, 3)
724
- def f (* args , z ): ... # f(1, 2, z=3)
725
- ```
726
-
727
- ``` python
728
- def f (** kwargs ): ... # f(x=1, y=2, z=3)
729
- def f (x , ** kwargs ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3)
730
- ```
731
-
718
+ #### Allowed compositions of arguments inside function definition and the ways they can be called:
732
719
``` python
733
- def f (* args , ** kwargs ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
734
- def f (x , * args , ** kwargs ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
735
- def f (* args , y , ** kwargs ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3)
736
- ```
737
-
738
- ``` python
739
- def f (* , x , y , z ): ... # f(x=1, y=2, z=3)
740
- def f (x , * , y , z ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3)
741
- def f (x , y , * , z ): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
720
+ + -------------------- + ------------ + -------------- + ---------------- + ------------------ +
721
+ | | f(1 , 2 , 3 ) | f(1 , 2 , z = 3 ) | f(1 , y = 2 , z = 3 ) | f(x = 1 , y = 2 , z = 3 ) |
722
+ + -------------------- + ------------ + -------------- + ---------------- + ------------------ +
723
+ | f(x, * args, ** kw): | yes | yes | yes | yes |
724
+ | f(* args, z, ** kw): | | yes | yes | yes |
725
+ | f(x, ** kw): | | | yes | yes |
726
+ | f(* , x, ** kw): | | | | yes |
727
+ + -------------------- + ------------ + -------------- + ---------------- + ------------------ +
742
728
```
743
729
744
730
### Other Uses
745
731
``` python
746
- < list > = [* < coll. > [, ... ]] # Or: list(<collection>) [+ ...]
747
- < tuple > = (* < coll. > , [... ]) # Or: tuple(<collection>) [+ ...]
748
- < set > = {* < coll. > [, ... ]} # Or: set(<collection>) [| ...]
749
- < dict > = {** < dict > [, ... ]} # Or: <dict> | ...
732
+ < list > = [* < collection > [, ... ]] # Or: list(<collection>) [+ ...]
733
+ < tuple > = (* < collection > , [... ]) # Or: tuple(<collection>) [+ ...]
734
+ < set > = {* < collection > [, ... ]} # Or: set(<collection>) [| ...]
735
+ < dict > = {** < dict > [, ... ]} # Or: <dict> | ...
750
736
```
751
737
752
738
``` python
753
- head, * body, tail = < coll. > # Head or tail can be omitted.
739
+ head, * body, tail = < collection > # Head or tail can be omitted.
754
740
```
755
741
756
742
@@ -798,24 +784,32 @@ from functools import reduce
798
784
```
799
785
800
786
``` python
801
- >> > [a if a else ' zero' for a in (0 , 1 , 2 , 3 )] # `any([0, '', [], None]) == False`
787
+ >> > [i if i else ' zero' for i in (0 , 1 , 2 , 3 )] # `any([0, '', [], None]) == False`
802
788
[' zero' , 1 , 2 , 3 ]
803
789
```
804
790
791
+ ### And, Or
792
+ ``` python
793
+ < obj> = < exp> and < exp> [and ... ] # Returns first false or last operand.
794
+ < obj> = < exp> or < exp> [or ... ] # Returns first true or last operand.
795
+ ```
796
+
797
+ ### Walrus Operator
798
+ ``` python
799
+ >> > [i for a in ' 0123' if (i := int (a)) > 0 ] # Assigns to variable mid-sentence.
800
+ [1 , 2 , 3 ]
801
+ ```
802
+
805
803
### Named Tuple, Enum, Dataclass
806
804
``` python
807
805
from collections import namedtuple
808
- Point = namedtuple(' Point' , ' x y' ) # Creates a tuple's subclass.
806
+ Point = namedtuple(' Point' , ' x y' ) # Creates tuple's subclass.
809
807
point = Point(0 , 0 ) # Returns its instance.
810
- ```
811
808
812
- ``` python
813
809
from enum import Enum
814
- Direction = Enum(' Direction' , ' N E S W' ) # Creates an enum .
810
+ Direction = Enum(' Direction' , ' N E S W' ) # Creates Enum's subclass .
815
811
direction = Direction.N # Returns its member.
816
- ```
817
812
818
- ``` python
819
813
from dataclasses import make_dataclass
820
814
Player = make_dataclass(' Player' , [' loc' , ' dir' ]) # Creates a class.
821
815
player = Player(point, direction) # Returns its instance.
0 commit comments