Skip to content

Commit 13af7a1

Browse files
author
Philip Guo
committed
added another mini-lesson
1 parent dc49f3e commit 13af7a1

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎v3/js/opt-lessons.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ $(document).ready(function() {
102102

103103
//$.get("lessons/aliasing.txt", parseLessonFile);
104104
//$.get("lessons/dive-into-python-311.txt", parseLessonFile);
105-
$.get("lessons/for-else.txt", parseLessonFile);
105+
//$.get("lessons/for-else.txt", parseLessonFile);
106+
$.get("lessons/varargs.txt", parseLessonFile);
106107

107108
// log a generic AJAX error handler
108109
$(document).ajaxError(function() {

‎v3/lessons/varargs.txt‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def f1(a, b, *rest):
2+
pass
3+
4+
f1(1, 2)
5+
f1(1, 2, 3, 4, 5, 6, 7)
6+
7+
def f2(a, b, **kwrest):
8+
pass
9+
10+
f2(1, 2, name='Bob', age=38)
11+
12+
def f3(a, b, *rest, **kwrest):
13+
pass
14+
15+
f3(1, 2, 3, 4, name='Bob', age=38)
16+
======
17+
{
18+
"title": "Optional Function Arguments",
19+
20+
"description": "Using <code>*</code> and <code>**</code> in argument names allows Python functions to accept an arbitrary number of optional arguments.",
21+
22+
"1": "<code>f1</code> is declared with two mandatory arguments (<code>a</code> and <code>b</code>) and an optional <code>*rest</code> argument.",
23+
24+
"3": "When you call <code>f1</code> with only the two mandatory arguments, <code>rest</code> is an empty tuple.",
25+
26+
"7": "When you call <code>f1</code> with additional arguments, <code>rest</code> is a tuple that holds the contents of everything after the two mandatory arguments.",
27+
28+
"10": "<code>f2</code> is declared with two mandatory arguments and an optional <code>**kwrest</code> argument.",
29+
30+
"12": "When you call <code>f2</code> with additional <i>keyword</i> arguments (e.g., <code>name='Bob', age=38</code>), <code>kwrest</code> becomes a dict bound to their respective keys and values.",
31+
32+
"15": "You can combine up to one <code>*</code> and one <code>**</code> argument in a function definition.",
33+
34+
"17": "Now any extra regular arguments (here, the numbers 3 and 4) are found in the <code>*rest</code> tuple, and any extra keyword arguments are in the <code>**kwrest</code> dict."
35+
}

0 commit comments

Comments
 (0)