Skip to content

Commit 159f6b9

Browse files
travisjneumanclaude
andcommitted
feat: add 10 intermediate Mermaid diagram files for concept docs
Add visual diagrams for imports, classes, decorators, virtual environments, comprehensions, args/kwargs, context managers, enums, type hints, and dataclasses. Each file contains 2-4 focused Mermaid diagrams with descriptive labels and consistent styling matching the existing 8 beginner diagrams. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2e89705 commit 159f6b9

10 files changed

+1105
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Diagrams: *args and **kwargs Explained
2+
3+
[Back to concept](../args-kwargs-explained.md)
4+
5+
---
6+
7+
## Argument Passing Flow
8+
9+
Python matches arguments to parameters by position first, then by name.
10+
11+
```mermaid
12+
flowchart TD
13+
CALL["greet('Alice', 'Bob', greeting='Hi')"]
14+
15+
subgraph MATCHING ["Python matches arguments"]
16+
POS["Positional args<br/>'Alice' and 'Bob'"]
17+
KW["Keyword arg<br/>greeting='Hi'"]
18+
end
19+
20+
subgraph FUNC ["def greet(*args, greeting='Hello')"]
21+
ARGS["args = ('Alice', 'Bob')"]
22+
KWARG["greeting = 'Hi'"]
23+
end
24+
25+
CALL --> MATCHING
26+
POS --> ARGS
27+
KW --> KWARG
28+
29+
style POS fill:#4a9eff,stroke:#2670c2,color:#fff
30+
style KW fill:#ff922b,stroke:#e8590c,color:#fff
31+
style ARGS fill:#4a9eff,stroke:#2670c2,color:#fff
32+
style KWARG fill:#ff922b,stroke:#e8590c,color:#fff
33+
```
34+
35+
## *args: Collecting Extra Positional Arguments
36+
37+
`*args` gathers any number of positional arguments into a tuple.
38+
39+
```mermaid
40+
flowchart LR
41+
subgraph CALL ["add(1, 2, 3, 4, 5)"]
42+
A1["1"]
43+
A2["2"]
44+
A3["3"]
45+
A4["4"]
46+
A5["5"]
47+
end
48+
49+
PACK["* packs them<br/>into a tuple"]
50+
51+
subgraph FUNC ["def add(*args)"]
52+
T["args = (1, 2, 3, 4, 5)"]
53+
LOOP["for num in args:<br/> total += num"]
54+
RES["return 15"]
55+
T --> LOOP --> RES
56+
end
57+
58+
A1 --> PACK
59+
A2 --> PACK
60+
A3 --> PACK
61+
A4 --> PACK
62+
A5 --> PACK
63+
PACK --> T
64+
65+
style CALL fill:#4a9eff,stroke:#2670c2,color:#fff
66+
style PACK fill:#ffd43b,stroke:#f59f00,color:#000
67+
style FUNC fill:#51cf66,stroke:#27ae60,color:#fff
68+
```
69+
70+
## **kwargs: Collecting Extra Keyword Arguments
71+
72+
`**kwargs` gathers any number of keyword arguments into a dictionary.
73+
74+
```mermaid
75+
flowchart LR
76+
subgraph CALL ["build_profile(name='Alice', age=30, city='NYC')"]
77+
K1["name='Alice'"]
78+
K2["age=30"]
79+
K3["city='NYC'"]
80+
end
81+
82+
PACK["** packs them<br/>into a dict"]
83+
84+
subgraph FUNC ["def build_profile(**kwargs)"]
85+
D["kwargs = {<br/> 'name': 'Alice',<br/> 'age': 30,<br/> 'city': 'NYC'<br/>}"]
86+
end
87+
88+
K1 --> PACK
89+
K2 --> PACK
90+
K3 --> PACK
91+
PACK --> D
92+
93+
style CALL fill:#ff922b,stroke:#e8590c,color:#fff
94+
style PACK fill:#ffd43b,stroke:#f59f00,color:#000
95+
style FUNC fill:#51cf66,stroke:#27ae60,color:#fff
96+
```
97+
98+
## Parameter Order Rule
99+
100+
When combining all parameter types, they must appear in this exact order.
101+
102+
```mermaid
103+
flowchart LR
104+
P1["1. Regular params<br/>def f(a, b)"]
105+
P2["2. *args<br/>def f(a, b, *args)"]
106+
P3["3. Keyword-only<br/>def f(a, *args, flag=True)"]
107+
P4["4. **kwargs<br/>def f(a, *args, flag=True, **kwargs)"]
108+
109+
P1 --> P2 --> P3 --> P4
110+
111+
style P1 fill:#4a9eff,stroke:#2670c2,color:#fff
112+
style P2 fill:#51cf66,stroke:#27ae60,color:#fff
113+
style P3 fill:#ffd43b,stroke:#f59f00,color:#000
114+
style P4 fill:#ff922b,stroke:#e8590c,color:#fff
115+
```
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Diagrams: Classes and Objects
2+
3+
[Back to concept](../classes-and-objects.md)
4+
5+
---
6+
7+
## Class Instantiation Sequence
8+
9+
When you create an object from a class, Python performs several steps behind the scenes.
10+
11+
```mermaid
12+
sequenceDiagram
13+
participant Code as Your Code
14+
participant Class as Dog class
15+
participant Init as __init__ method
16+
participant Obj as New Object
17+
18+
Code->>Class: Dog("Rex", 5)
19+
Note over Class: Python creates a<br/>new empty object
20+
Class->>Init: __init__(self, name, age)
21+
Note over Init: self.name = "Rex"
22+
Note over Init: self.age = 5
23+
Init-->>Class: Object is initialized
24+
Class-->>Code: Return the Dog object
25+
Note over Code: my_dog = Dog("Rex", 5)<br/>my_dog.name is "Rex"
26+
```
27+
28+
## Inheritance Hierarchy
29+
30+
Child classes inherit attributes and methods from parent classes, and can add or override them.
31+
32+
```mermaid
33+
classDiagram
34+
class Animal {
35+
+name: str
36+
+sound: str
37+
+speak() str
38+
+eat() str
39+
}
40+
41+
class Dog {
42+
+breed: str
43+
+speak() str
44+
+fetch() str
45+
}
46+
47+
class Cat {
48+
+indoor: bool
49+
+speak() str
50+
+purr() str
51+
}
52+
53+
class GuideDog {
54+
+handler: str
55+
+guide() str
56+
}
57+
58+
Animal <|-- Dog : inherits
59+
Animal <|-- Cat : inherits
60+
Dog <|-- GuideDog : inherits
61+
62+
note for Dog "Overrides speak()&#10;Adds fetch()"
63+
note for GuideDog "Inherits from Dog&#10;AND from Animal"
64+
```
65+
66+
## Method Resolution Order (MRO)
67+
68+
With multiple inheritance, Python searches for methods in a specific order called the MRO.
69+
70+
```mermaid
71+
flowchart TD
72+
CALL["guide_dog.speak()"] --> STEP1{"1. GuideDog<br/>has speak()?"}
73+
STEP1 -->|No| STEP2{"2. Dog<br/>has speak()?"}
74+
STEP1 -->|Yes| FOUND1(["Use GuideDog.speak()"])
75+
STEP2 -->|Yes| FOUND2(["Use Dog.speak()"])
76+
STEP2 -->|No| STEP3{"3. Animal<br/>has speak()?"}
77+
STEP3 -->|Yes| FOUND3(["Use Animal.speak()"])
78+
STEP3 -->|No| STEP4{"4. object<br/>(base of everything)"}
79+
STEP4 -->|Not found| ERR["AttributeError!"]
80+
81+
style STEP1 fill:#4a9eff,stroke:#2670c2,color:#fff
82+
style STEP2 fill:#51cf66,stroke:#27ae60,color:#fff
83+
style STEP3 fill:#ffd43b,stroke:#f59f00,color:#000
84+
style STEP4 fill:#cc5de8,stroke:#9c36b5,color:#fff
85+
style ERR fill:#ff6b6b,stroke:#c0392b,color:#fff
86+
```
87+
88+
## Class vs Instance Attributes
89+
90+
Class attributes are shared by all objects. Instance attributes belong to one object.
91+
92+
```mermaid
93+
flowchart TD
94+
subgraph CLASS ["Dog class"]
95+
CA["species = 'Canis familiaris'<br/>(shared by ALL dogs)"]
96+
end
97+
98+
subgraph OBJ1 ["dog1 = Dog('Rex', 5)"]
99+
I1A["name = 'Rex'"]
100+
I1B["age = 5"]
101+
end
102+
103+
subgraph OBJ2 ["dog2 = Dog('Bella', 3)"]
104+
I2A["name = 'Bella'"]
105+
I2B["age = 3"]
106+
end
107+
108+
CLASS -->|"dog1.species"| OBJ1
109+
CLASS -->|"dog2.species"| OBJ2
110+
111+
style CLASS fill:#ff922b,stroke:#e8590c,color:#fff
112+
style OBJ1 fill:#4a9eff,stroke:#2670c2,color:#fff
113+
style OBJ2 fill:#51cf66,stroke:#27ae60,color:#fff
114+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Diagrams: Comprehensions Explained
2+
3+
[Back to concept](../comprehensions-explained.md)
4+
5+
---
6+
7+
## List Comprehension Data Flow
8+
9+
A list comprehension takes an iterable, optionally filters it, transforms each item, and collects results.
10+
11+
```mermaid
12+
flowchart LR
13+
INPUT["Input iterable<br/>[1, 2, 3, 4, 5, 6]"]
14+
FILTER["Filter (optional)<br/>if x % 2 == 0"]
15+
TRANSFORM["Transform<br/>x ** 2"]
16+
OUTPUT["Output list<br/>[4, 16, 36]"]
17+
18+
INPUT --> FILTER --> TRANSFORM --> OUTPUT
19+
20+
style INPUT fill:#4a9eff,stroke:#2670c2,color:#fff
21+
style FILTER fill:#ffd43b,stroke:#f59f00,color:#000
22+
style TRANSFORM fill:#cc5de8,stroke:#9c36b5,color:#fff
23+
style OUTPUT fill:#51cf66,stroke:#27ae60,color:#fff
24+
```
25+
26+
## Comprehension vs Loop: Side by Side
27+
28+
Every comprehension can be rewritten as a for loop. The comprehension is just more concise.
29+
30+
```mermaid
31+
flowchart TD
32+
subgraph LOOP ["For Loop (4 lines)"]
33+
L1["squares = []"]
34+
L2["for x in range(5):"]
35+
L3[" squares.append(x ** 2)"]
36+
L4["# squares = [0, 1, 4, 9, 16]"]
37+
L1 --> L2 --> L3 --> L4
38+
end
39+
40+
subgraph COMP ["Comprehension (1 line)"]
41+
C1["squares = [x ** 2 for x in range(5)]"]
42+
C2["# squares = [0, 1, 4, 9, 16]"]
43+
C1 --> C2
44+
end
45+
46+
LOOP ---|"Same result,<br/>less code"| COMP
47+
48+
style LOOP fill:#ffd43b,stroke:#f59f00,color:#000
49+
style COMP fill:#51cf66,stroke:#27ae60,color:#fff
50+
```
51+
52+
## Anatomy of a Comprehension
53+
54+
Breaking down the parts of `[expression for item in iterable if condition]`.
55+
56+
```mermaid
57+
flowchart TD
58+
FULL["[x * 2 for x in range(10) if x > 3]"]
59+
FULL --> PART_OUT["x * 2<br/>OUTPUT expression<br/>What goes into the result"]
60+
FULL --> PART_FOR["for x in range(10)<br/>ITERATION<br/>What we loop over"]
61+
FULL --> PART_IF["if x > 3<br/>FILTER (optional)<br/>Which items to keep"]
62+
63+
PART_FOR -->|"x = 0, 1, 2, ... 9"| PART_IF
64+
PART_IF -->|"Keeps: 4, 5, 6, 7, 8, 9"| PART_OUT
65+
PART_OUT -->|"Result"| RESULT["[8, 10, 12, 14, 16, 18]"]
66+
67+
style PART_OUT fill:#51cf66,stroke:#27ae60,color:#fff
68+
style PART_FOR fill:#4a9eff,stroke:#2670c2,color:#fff
69+
style PART_IF fill:#ffd43b,stroke:#f59f00,color:#000
70+
style RESULT fill:#cc5de8,stroke:#9c36b5,color:#fff
71+
```
72+
73+
## List vs Dict vs Set Comprehension
74+
75+
The brackets you use determine what type of collection you get.
76+
77+
```mermaid
78+
flowchart TD
79+
subgraph LIST_COMP ["List Comprehension [ ]"]
80+
LC["[x**2 for x in range(4)]"]
81+
LR2["Result: [0, 1, 4, 9]"]
82+
LC --> LR2
83+
end
84+
85+
subgraph DICT_COMP ["Dict Comprehension { : }"]
86+
DC["{x: x**2 for x in range(4)}"]
87+
DR["Result: {0: 0, 1: 1, 2: 4, 3: 9}"]
88+
DC --> DR
89+
end
90+
91+
subgraph SET_COMP ["Set Comprehension { }"]
92+
SC["{x % 3 for x in range(6)}"]
93+
SR["Result: {0, 1, 2}"]
94+
SC --> SR
95+
end
96+
97+
style LIST_COMP fill:#4a9eff,stroke:#2670c2,color:#fff
98+
style DICT_COMP fill:#ff922b,stroke:#e8590c,color:#fff
99+
style SET_COMP fill:#cc5de8,stroke:#9c36b5,color:#fff
100+
```

0 commit comments

Comments
 (0)