Repository navigation
Expand file tree
/
Copy path07_function_as_data.py
More file actions
84 lines (61 loc) · 2.9 KB
/
Copy path07_function_as_data.py
File metadata and controls
84 lines (61 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Module 7 - Treat functions as data in a small processing pipeline."""
# 1. Real-World Scenario
# A report can apply different scoring rules without rewriting the pipeline.
# 2. The Challenge
scores = [40, 65, 90]
# Build a function that accepts another function and applies it to every score.
# 3. My Current Python Toolkit
# Functions are objects; a list can store them; a loop can call each one.
# 4. Think Before Coding
# What input/output contract should each rule follow? Who chooses the rule?
# 5. Try It Yourself
# Create apply_rule(values, rule) and test it with a bonus rule.
# 6. Solution
def apply_rule_a(values, rule):
result = []
for value in values:
result.append(rule(value))
return result
def bonus(score):
return score + 5
print('Version A:', apply_rule_a(scores, bonus))
apply_rule_b = lambda values, rule: [rule(value) for value in values]
print('Version B:', apply_rule_b(scores, lambda score: score * 2))
# A named function is clearer for reusable logic; a lambda is fine for a tiny one-off rule.
# 7. Trace It Visually
# [40, 65, 90] -> bonus(40), bonus(65), bonus(90) -> [45, 70, 95].
# 8. What Is Actually Happening Internally?
# The caller passes a function reference. The processing function calls it with one value.
# 9. THE IMPORTANT PART - THIS BECOMES...
# Python: function as a value
# Backend: configurable validation or transformation
# Production: pipeline stages and callbacks
# AI / LLM: model/provider strategy chosen at runtime
# Advanced System: plugin or agent tool registry [LEARN LATER]
# 10. Future Code Example
def run_model(text, model_function):
return {'text': text, 'answer': model_function(text)}
print('Model strategy:', run_model('hello', lambda text: text.upper()))
# 11. Mind-Bending Questions
questions = [
'What does it mean to pass a function as an argument?',
'Why should all rules return a compatible type?',
'When is a lambda less readable than def?',
'How could you select a model function from a dictionary?',
'What security check is needed before loading external functions?',
]
for number, question in enumerate(questions, 1):
print(f'Q{number}: {question}')
# Answers
print('Answers: a function is an object reference; consistent output keeps pipelines composable; use def for named logic; map names to functions; validate allowed tools.')
# 12. Interview Connection
print('Interview: first-class functions, higher-order functions, callbacks, and strategy design.')
# 13. Common Mistakes
# WRONG: apply_rule_b(scores, bonus()) # calls bonus immediately with no argument.
# CORRECT: apply_rule_b(scores, bonus)
# WRONG: rules = [bonus()] # stores a result, not a function.
# CORRECT: rules = [bonus]
# 14. Small Extension
# Add a validation rule, a logging wrapper, and a dictionary of named strategies.
# 15. Connection Graph
# function object -> callback -> configurable pipeline -> model strategy -> agent tool registry