Skip to content

Commit 020bbb1

Browse files
committed
Renamed Arguments to Function, big changes in Splat and Inline
1 parent 889b34f commit 020bbb1

File tree

6 files changed

+142
-127
lines changed

6 files changed

+142
-127
lines changed

README.md

+49-55
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Contents
1010
--------
1111
**   ** **1. Collections:** ** ** **[`List`](#list)**__,__ **[`Dictionary`](#dictionary)**__,__ **[`Set`](#set)**__,__ **[`Tuple`](#tuple)**__,__ **[`Range`](#range)**__,__ **[`Enumerate`](#enumerate)**__,__ **[`Iterator`](#iterator)**__,__ **[`Generator`](#generator)**__.__
1212
**   ** **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)**__.__
1414
**   ** **4. System:** **        ** **[`Exit`](#exit)**__,__ **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#paths)**__,__ **[`OS_Commands`](#os-commands)**__.__
1515
**   ** **5. Data:** **             ** **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`CSV`](#csv)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`Memory_View`](#memory-view)**__,__ **[`Deque`](#deque)**__.__
1616
**   ** **6. Advanced:** **   ** **[`Operator`](#operator)**__,__ **[`Match_Stmt`](#match-statement)**__,__ **[`Logging`](#logging)**__,__ **[`Introspection`](#introspection)**__,__ **[`Threading`](#threading)**__,__ **[`Coroutines`](#coroutines)**__.__
@@ -671,86 +671,72 @@ import zoneinfo, dateutil.tz
671671
```
672672

673673

674-
Arguments
675-
---------
676-
### Inside Function Call
674+
Function
675+
--------
676+
**Independent block of code that returns a value when called.**
677677
```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): ...`.
681681
```
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
682687

683-
### Inside Function Definition
684688
```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)`.
688692
```
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!**
691693

692694

693695
Splat Operator
694696
--------------
695-
### Inside Function Call
696697
**Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.**
697698
```python
698-
args = (1, 2)
699-
kwargs = {'x': 3, 'y': 4, 'z': 5}
699+
args, kwargs = (1, 2), {'z': 3}
700700
func(*args, **kwargs)
701701
```
702702

703703
#### Is the same as:
704704
```python
705-
func(1, 2, x=3, y=4, z=5)
705+
func(1, 2, z=3)
706706
```
707707

708708
### Inside Function Definition
709709
**Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.**
710710
```python
711-
def add(*a):
712-
return sum(a)
713-
```
714-
715-
```python
711+
>>> def add(*a):
712+
... return sum(a)
713+
...
716714
>>> add(1, 2, 3)
717715
6
718716
```
719717

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:
732719
```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+
+--------------------+------------+--------------+----------------+------------------+
742728
```
743729

744730
### Other Uses
745731
```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> | ...
750736
```
751737

752738
```python
753-
head, *body, tail = <coll.> # Head or tail can be omitted.
739+
head, *body, tail = <collection> # Head or tail can be omitted.
754740
```
755741

756742

@@ -798,24 +784,32 @@ from functools import reduce
798784
```
799785

800786
```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`
802788
['zero', 1, 2, 3]
803789
```
804790

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+
805803
### Named Tuple, Enum, Dataclass
806804
```python
807805
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.
809807
point = Point(0, 0) # Returns its instance.
810-
```
811808

812-
```python
813809
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.
815811
direction = Direction.N # Returns its member.
816-
```
817812

818-
```python
819813
from dataclasses import make_dataclass
820814
Player = make_dataclass('Player', ['loc', 'dir']) # Creates a class.
821815
player = Player(point, direction) # Returns its instance.

0 commit comments

Comments
 (0)