You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ea3d39c
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: _sources/index.rst.txt
+13-8Lines changed: 13 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,24 +13,29 @@ This site holds many of the materials for the
13
13
14
14
This site can be thought of as the textbook for Programming in Python: the first course in the program. It contains notes about the topics covered in the classes, programming exercises, supplemental materials about setting up a development environment, and assorted references about Python-related topics.
Copy file name to clipboardExpand all lines: _sources/modules/Coroutines.rst.txt
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ So calling ``send`` ran the print statement, and then raise a ``StopIteration``
91
91
92
92
StopIteration:
93
93
94
-
And indeed, they have a lot on common -- in fact, before Python3.5, when the ``async`` keyword was added, you used generators to make coroutines.
94
+
And indeed, they have a lot on common -- in fact, before Python3.5, when the ``async`` keyword was added, you used generator functions to make coroutines.
95
95
96
96
But if a coroutine raises ``StopIteration`` right away, what's the point? Well, recall that the point of a coroutine (and asnyc in general), is to be able to return control to the system, while you wait for something else to happen. And thus the "await" keyword. So a coroutine isn't useful unless it uses ``await``
Copy file name to clipboardExpand all lines: _sources/modules/IteratorsAndGenerators.rst.txt
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,9 @@ Iterators and Generators
11
11
A note about Python History
12
12
---------------------------
13
13
14
+
Python 2
15
+
.........
16
+
14
17
Python used to be all about sequences -- a good chunk of anything you did
15
18
was stored in a sequence, or involved manipulating a sequence.
16
19
@@ -24,7 +27,7 @@ was stored in a sequence, or involved manipulating a sequence.
24
27
- ``zip()``
25
28
- ...
26
29
27
-
In python2 -- those are all sequences.
30
+
In python2 -- those are all sequences. (in the case of zip and dict methods, they return actual lists)
28
31
29
32
But it turns out that the most common operation for sequences is to iterate through them:
30
33
@@ -50,10 +53,10 @@ But why make a full copy of all the keys, when all you want to do is:
50
53
for k indict.keys():
51
54
do_something_with(k)
52
55
53
-
Even worse: ``dict.items()`` created a full list of ``(key,value)`` tuples.
56
+
Even worse: ``dict.items()`` created a full list of ``(key,value)`` tuples.
54
57
-- a complete copy of all the data in the dict.
55
58
56
-
Even worse: ``enumerate(dict.items())`` created a whole list of
59
+
Even worse still: ``enumerate(dict.items())`` created a whole list of
57
60
``(index, (key, value))`` tuples -- lots of copies of everything.
58
61
59
62
Enter ``iter*``
@@ -65,27 +68,25 @@ Python2 then introduced "iterable" versions of a number of functions and methods
65
68
``dict.iterkeys()``
66
69
``dict.itervalues()``
67
70
68
-
So you could now iterate through that stuff without copying anything.
69
-
70
-
**Python3**
71
+
So you could now iterate through that stuff without copying anything. Nice performance benefits, but a somewhat ugly interface.
71
72
72
-
Python3 embraces iterables -- now everything that could be an iterator
73
-
is already an iterator -- no unnecessary copies.
73
+
Python3
74
+
.......
74
75
75
-
An iterator is an iterable that has been made more efficient by
76
-
removing as much from memory as possible.
76
+
Python3 embraces iterables -- now everything that could be an iterable without making a copy is done that way -- no unnecessary copies.
77
77
78
-
You have to make a list out of them explicitly if you really want it:
78
+
If you DO need an actual sequence (becasue you want to do something with it other than iterate over it), you have to make a list out of them explicitly:
79
79
80
80
``list(dict.keys())``
81
81
82
82
Then there is an entire module: ``itertools`` that provides nifty ways
83
83
to iterate through stuff.
84
84
85
-
We will visit this again soon.
85
+
That will be covered elsewhere.
86
+
87
+
So while I used to say that python was "all about sequences", I know say:
86
88
87
-
So while I used to say that python was all about sequences
<liclass="toctree-l2"><aclass="reference internal" href="#lesson-1-introductions-and-setting-up-your-environment">Lesson 1: Introductions and Setting up your Environment</a><ul>
0 commit comments