From 52fd3afafc146f6b60df4e5fcecebfaf1b8fa806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 29 Sep 2019 17:18:43 +0200 Subject: [PATCH] Iterator --- README.md | 16 ++++++---------- index.html | 19 +++++++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9f965e5..c79e428 100644 --- a/README.md +++ b/README.md @@ -1144,6 +1144,12 @@ class Counter: (1, 2, 3) ``` +#### Python has many different iterator objects: +* **Iterators returned by the [iter()](#iterator) function, such as list\_iterator and set\_iterator.** +* **Objects returned by the [itertools](#itertools) module, such as count, repeat and cycle.** +* **Generators returned by the [generator functions](#generator) and [generator expressions](#comprehension).** +* **All [file objects](#file), etc.** + ### Callable * **All functions and classes have a call() method, hence are callable.** * **When this cheatsheet uses `''` for an argument, it actually means `''`.** @@ -1184,16 +1190,6 @@ class MyOpen(): Hello World! ``` -#### List of covered context managers: -```python -with open('') as file: ... -with wave.open('') as wave_file: ... -with memoryview() as view: ... -with concurrent.futures.ThreadPoolExecutor() as executor: ... -db = sqlite3.connect(''); with db: ... -lock = threading.RLock(); with lock: ... -``` - Iterable Duck Types ------------------- diff --git a/index.html b/index.html index 72b50d8..b98bfbc 100644 --- a/index.html +++ b/index.html @@ -1082,7 +1082,12 @@ Z = dataclasses.make_dataclass('Z', [>>> next(counter), next(counter), next(counter) (1, 2, 3) -

Callable

    +

    Python has many different iterator objects:

    Callable

    • All functions and classes have a call() method, hence are callable.
    • When this cheatsheet uses '<function>' for an argument, it actually means '<callable>'.
    class Counter:
    @@ -1091,7 +1096,9 @@ Z = dataclasses.make_dataclass('Z', [def __call__(self):
             self.i += 1
             return self.i
    -
    +
+ +
>>> counter = Counter()
@@ -1118,14 +1125,6 @@ Z = dataclasses.make_dataclass('Z', [...     print(file.read())
 Hello World!
 
-

List of covered context managers:

with open('<path>') as file: ...
-with wave.open('<path>') as wave_file: ...
-with memoryview(<bytes/bytearray/array>) as view: ...
-with concurrent.futures.ThreadPoolExecutor() as executor: ...
-db = sqlite3.connect('<path>'); with db: ...
-lock = threading.RLock(); with lock: ...
-
-

#Iterable Duck Types

Iterable

  • Only required method is iter(). It should return an iterator of object's items.
  • Contains() automatically works on any object that has iter() defined.