Skip to content

Commit 50d3ee8

Browse files
added docs
1 parent 8198763 commit 50d3ee8

15 files changed

Lines changed: 1057 additions & 0 deletions
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,375 @@
1+
# Callable and Functions in Python
2+
3+
# Functions Are First-Class Objects
4+
5+
In Python, functions are treated like any other object.
6+
7+
This means functions can:
8+
- be stored in variables
9+
- be passed as arguments
10+
- be returned from other functions
11+
- be stored inside lists/dictionaries
12+
13+
---
14+
15+
# Function Object vs Function Call
16+
17+
This is one of the most important concepts in functional programming.
18+
19+
---
20+
21+
# Function Object
22+
23+
```python
24+
def greet():
25+
return "Hello"
26+
27+
28+
print(greet)
29+
```
30+
31+
## Output
32+
33+
```text
34+
<function greet at 0x...>
35+
```
36+
37+
Here we are referencing the function itself.
38+
39+
No execution happens.
40+
41+
---
42+
43+
# Function Call
44+
45+
```python
46+
def greet():
47+
return "Hello"
48+
49+
50+
print(greet())
51+
```
52+
53+
## Output
54+
55+
```text
56+
Hello
57+
```
58+
59+
The parentheses `()` execute the function.
60+
61+
---
62+
63+
# Visual Difference
64+
65+
| Code | Meaning |
66+
|---|---|
67+
| greet | Function object |
68+
| greet() | Function call / execution |
69+
70+
---
71+
72+
# Storing Functions in Variables
73+
74+
```python
75+
def fireball():
76+
return "Fireball cast!"
77+
78+
79+
spell = fireball
80+
81+
print(spell())
82+
```
83+
84+
## Output
85+
86+
```text
87+
Fireball cast!
88+
```
89+
90+
The variable stores the function itself.
91+
92+
---
93+
94+
# Passing Functions as Arguments
95+
96+
Functions can be passed into other functions.
97+
98+
---
99+
100+
# Example
101+
102+
```python
103+
def greet(name):
104+
return f"Hello {name}"
105+
106+
107+
def execute(func, value):
108+
return func(value)
109+
110+
111+
print(execute(greet, "Sara"))
112+
```
113+
114+
## Output
115+
116+
```text
117+
Hello Sara
118+
```
119+
120+
---
121+
122+
# Why Is This Powerful?
123+
124+
This allows:
125+
- reusable logic
126+
- decorators
127+
- callbacks
128+
- flexible systems
129+
- functional programming patterns
130+
131+
---
132+
133+
# Returning Functions
134+
135+
Functions can also return other functions.
136+
137+
---
138+
139+
# Example
140+
141+
```python
142+
def multiplier(x):
143+
144+
def inner(y):
145+
return x * y
146+
147+
return inner
148+
149+
150+
double = multiplier(2)
151+
152+
print(double(5))
153+
```
154+
155+
## Output
156+
157+
```text
158+
10
159+
```
160+
161+
---
162+
163+
# Visual Representation
164+
165+
```text
166+
multiplier(2)
167+
168+
└── returns inner()
169+
170+
└── remembers x = 2
171+
```
172+
173+
This is the basis of closures.
174+
175+
---
176+
177+
# What Is `Callable`?
178+
179+
`Callable` is a type hint used for functions.
180+
181+
It comes from:
182+
183+
```python
184+
from collections.abc import Callable
185+
```
186+
187+
---
188+
189+
# Basic Callable Example
190+
191+
```python
192+
from collections.abc import Callable
193+
194+
195+
def execute(func: Callable, value: int) -> int:
196+
return func(value)
197+
```
198+
199+
---
200+
201+
# Callable With Parameters and Return Types
202+
203+
You can specify:
204+
- argument types
205+
- return type
206+
207+
---
208+
209+
# Syntax
210+
211+
```python
212+
Callable[[arg1_type, arg2_type], return_type]
213+
```
214+
215+
---
216+
217+
# Example
218+
219+
```python
220+
from collections.abc import Callable
221+
222+
223+
def operation(func: Callable[[int, int], int]) -> int:
224+
return func(2, 3)
225+
```
226+
227+
---
228+
229+
# What Does `callable()` Do?
230+
231+
`callable()` checks if an object can be called like a function.
232+
233+
---
234+
235+
# Example
236+
237+
```python
238+
def greet():
239+
pass
240+
241+
242+
print(callable(greet))
243+
print(callable(42))
244+
```
245+
246+
## Output
247+
248+
```text
249+
True
250+
False
251+
```
252+
253+
---
254+
255+
# Objects Can Also Be Callable
256+
257+
Classes using `__call__()` become callable.
258+
259+
---
260+
261+
# Example
262+
263+
```python
264+
class Spell:
265+
266+
def __call__(self):
267+
return "Magic!"
268+
269+
270+
spell = Spell()
271+
272+
print(callable(spell))
273+
print(spell())
274+
```
275+
276+
## Output
277+
278+
```text
279+
True
280+
Magic!
281+
```
282+
283+
---
284+
285+
# Functions Inside Data Structures
286+
287+
Functions can be stored inside:
288+
- lists
289+
- tuples
290+
- dictionaries
291+
292+
---
293+
294+
# Example
295+
296+
```python
297+
def fire():
298+
return "Fire"
299+
300+
301+
def ice():
302+
return "Ice"
303+
304+
305+
spells = {
306+
"fire": fire,
307+
"ice": ice
308+
}
309+
310+
print(spells["fire"]())
311+
```
312+
313+
## Output
314+
315+
```text
316+
Fire
317+
```
318+
319+
---
320+
321+
# Common Mistakes
322+
323+
## Returning a Function Call Instead of the Function
324+
325+
Wrong:
326+
327+
```python
328+
return greet()
329+
```
330+
331+
Correct:
332+
333+
```python
334+
return greet
335+
```
336+
337+
---
338+
339+
# Forgetting Parentheses
340+
341+
Wrong:
342+
343+
```python
344+
print(greet)
345+
```
346+
347+
Correct:
348+
349+
```python
350+
print(greet())
351+
```
352+
353+
---
354+
355+
# Real Use Cases
356+
357+
These concepts are heavily used in:
358+
- decorators
359+
- callbacks
360+
- event systems
361+
- retry systems
362+
- Flask/FastAPI routes
363+
- functional programming
364+
- ML libraries
365+
366+
---
367+
368+
# Key Takeaways
369+
370+
- Functions are first-class objects in Python
371+
- Functions can be passed, stored, and returned
372+
- `Callable` is used for function type hints
373+
- `callable()` checks if an object can be executed
374+
- `function` and `function()` are very different
375+
- Closures and decorators rely heavily on these concepts

0 commit comments

Comments
 (0)