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
Copy file name to clipboardExpand all lines: concepts/generators/about.md
+27-2Lines changed: 27 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,9 +35,33 @@ The rationale behind this is that you use a generator when you do not need all t
35
35
36
36
This saves memory and processing power, since only the value you are _currently working on_ is calculated.
37
37
38
+
38
39
## Using a generator
39
40
40
-
Generators may be used in place of most `iterables` in Python. This includes _functions_ or _objects_ that require an `iterable`/`iterator` as an argument.
41
+
Generators (_technically [`generator-iterator`s][generator-iterator] — see the note below._) are a type of `iterator` and can be used anywhere in Python where an `iterator` or `iterable` is expected.
42
+
This includes _functions_ or _objects_ that require an `iterable`/`iterator` as an argument.
43
+
For a deeper dive, see [How to Make an Iterator in Python][how-to-iterator].
44
+
45
+
46
+
~~~~exercism/note
47
+
48
+
Generator-iterators are a special sub-set of [iterators][iterator].
49
+
`Iterators` are the mechanism/protocol that enables looping over _iterables_.
50
+
Generator-iterators and the iterators returned by common Python [`iterables`][iterables] act very similarly, but there are some important differences to note:
51
+
52
+
- They are _[lazily evaluated][lazy evaluation]_; iteration is _one-way_ and there is no "backing up" to a previous value.
53
+
- They are _consumed_ by iterating over the returned values; there is no resetting or saving in memory.
54
+
- They are not sortable and cannot be reversed.
55
+
- They are not sequence types, and _do not_ have `indexes`.
56
+
You cannot reference a previous or future value using addition or subtraction and you cannot use bracket (`[]`) notation or slicing.
57
+
- They cannot be used with the `len()` function, as they have no length.
58
+
- They can be _finite_ or _infinite_ - be careful when collecting all values from an _infinite_ `generator-iterator`!
Copy file name to clipboardExpand all lines: concepts/unpacking-and-multiple-assignment/about.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ A very common example of this behavior is `for item in list`, where `item` takes
8
8
This allows for code to be more concise and readable, and is done by separating the variables to be assigned with a comma such as `first, second, third = (1,2,3)` or `for index, item in enumerate(iterable)`.
9
9
10
10
The special operators `*` and `**` are often used in unpacking contexts.
11
-
`*` can be used to combine multiple `lists`/`tuples` into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
11
+
`*` can be used to combine multiple `list`s/`tuple`s into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
12
12
`**` can be used to combine multiple dictionaries into one dictionary by _unpacking_ each into a new common `dict`.
13
13
14
14
When the `*` operator is used without a collection, it _packs_ a number of values into a `list`.
@@ -73,7 +73,7 @@ Since `tuples` are immutable, you can't swap elements in a `tuple`.
73
73
The examples below use `lists` but the same concepts apply to `tuples`.
74
74
~~~~
75
75
76
-
In Python, it is possible to [unpack the elements of `list`/`tuple`/`dictionary`][unpacking] into distinct variables.
76
+
In Python, it is possible to [unpack the elements of a `list`/`tuple`/`dict`][unpacking] into distinct variables.
77
77
Since values appear within `lists`/`tuples` in a specific order, they are unpacked into variables in the same order:
78
78
79
79
```python
@@ -94,7 +94,7 @@ If there are values that are not needed then you can use `_` to flag them:
94
94
95
95
### Deep unpacking
96
96
97
-
Unpacking and assigning values from a `list`/`tuple` inside of a `list` or `tuple` (_also known as nested lists/tuples_), works in the same way a shallow unpacking does, but often needs qualifiers to clarify the values context or position:
97
+
Unpacking and assigning values from a `list`/`tuple` inside of a `list` or `tuple` (_also known as nested lists/tuples_), works in the same way a shallow unpacking does, but often needs qualifiers to clarify the value's context or position:
Copy file name to clipboardExpand all lines: concepts/unpacking-and-multiple-assignment/introduction.md
+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
@@ -8,7 +8,7 @@ A very common example of this behavior is `for item in list`, where `item` takes
8
8
This allows for code to be more concise and readable, and is done by separating the variables to be assigned with a comma such as `first, second, third = (1,2,3)` or `for index, item in enumerate(iterable)`.
9
9
10
10
The special operators `*` and `**` are often used in unpacking contexts.
11
-
`*` can be used to combine multiple `lists`/`tuples` into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
11
+
`*` can be used to combine multiple `list`s/`tuple`s into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
12
12
`**` can be used to combine multiple dictionaries into one dictionary by _unpacking_ each into a new common `dict`.
13
13
14
14
When the `*` operator is used without a collection, it _packs_ a number of values into a `list`.
0 commit comments