| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
def gen(): |
| 5 |
if random.choice([0, 1]): |
| 6 |
yield 1 |
| 7 |
else: |
| 8 |
yield "" |
| 9 |
|
| 10 |
gen_exe = gen() |
| 11 |
|
| 12 |
next(gen_exe) |
| 13 |
|
| 14 |
|
| 15 |
next(gen_exe, list) |
| 16 |
|
| 17 |
|
| 18 |
def gen_ret(value): |
| 19 |
yield value |
| 20 |
|
| 21 |
|
| 22 |
next(gen_ret(1)) |
| 23 |
|
| 24 |
|
| 25 |
next(gen_ret()). |
| 26 |
|
| 27 |
|
| 28 |
a = '' |
| 29 |
if gen_ret(): |
| 30 |
a = 3 |
| 31 |
|
| 32 |
a |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
def get(param): |
| 39 |
if random.choice([0, 1]): |
| 40 |
yield 1 |
| 41 |
else: |
| 42 |
yield "" |
| 43 |
|
| 44 |
|
| 45 |
get()[0]. |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
for a in get(): |
| 51 |
|
| 52 |
a |
| 53 |
|
| 54 |
|
| 55 |
class Get(): |
| 56 |
def __iter__(self): |
| 57 |
if random.choice([0, 1]): |
| 58 |
yield 1 |
| 59 |
else: |
| 60 |
yield "" |
| 61 |
|
| 62 |
b = [] |
| 63 |
for a in Get(): |
| 64 |
|
| 65 |
a |
| 66 |
b += [a] |
| 67 |
|
| 68 |
|
| 69 |
b |
| 70 |
|
| 71 |
b[0] |
| 72 |
|
| 73 |
g = iter(Get()) |
| 74 |
|
| 75 |
next(g) |
| 76 |
|
| 77 |
g = iter([1.0]) |
| 78 |
|
| 79 |
next(g) |
| 80 |
|
| 81 |
x, y = Get() |
| 82 |
|
| 83 |
x |
| 84 |
|
| 85 |
x |
| 86 |
|
| 87 |
class Iter: |
| 88 |
def __iter__(self): |
| 89 |
yield "" |
| 90 |
i = 0 |
| 91 |
while True: |
| 92 |
v = 1 |
| 93 |
yield v |
| 94 |
i += 1 |
| 95 |
a, b, c = Iter() |
| 96 |
|
| 97 |
a |
| 98 |
|
| 99 |
b |
| 100 |
|
| 101 |
c |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
class Counter: |
| 108 |
def __init__(self, low, high): |
| 109 |
self.current = low |
| 110 |
self.high = high |
| 111 |
|
| 112 |
def __iter__(self): |
| 113 |
return self |
| 114 |
|
| 115 |
def next(self): |
| 116 |
""" need to have both __next__ and next, because of py2/3 testing """ |
| 117 |
return self.__next__() |
| 118 |
|
| 119 |
def __next__(self): |
| 120 |
if self.current > self.high: |
| 121 |
raise StopIteration |
| 122 |
else: |
| 123 |
self.current += 1 |
| 124 |
return self.current - 1 |
| 125 |
|
| 126 |
|
| 127 |
for c in Counter(3, 8): |
| 128 |
|
| 129 |
print c |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
def gen(): |
| 136 |
if random.choice([0,1]): |
| 137 |
yield 1, "" |
| 138 |
else: |
| 139 |
yield 2, 1.0 |
| 140 |
|
| 141 |
|
| 142 |
a, b = next(gen()) |
| 143 |
|
| 144 |
a |
| 145 |
|
| 146 |
b |
| 147 |
|
| 148 |
|
| 149 |
def simple(): |
| 150 |
if random.choice([0, 1]): |
| 151 |
yield 1 |
| 152 |
else: |
| 153 |
yield "" |
| 154 |
|
| 155 |
a, b = simple() |
| 156 |
|
| 157 |
a |
| 158 |
|
| 159 |
|
| 160 |
b |
| 161 |
|
| 162 |
|
| 163 |
def simple2(): |
| 164 |
yield 1 |
| 165 |
yield "" |
| 166 |
|
| 167 |
a, b = simple2() |
| 168 |
|
| 169 |
a |
| 170 |
|
| 171 |
b |
| 172 |
|
| 173 |
a, = (a for a in [1]) |
| 174 |
|
| 175 |
a |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
gen().close.__call__ |
| 184 |
|
| 185 |
|
| 186 |
gen().throw() |
| 187 |
|
| 188 |
|
| 189 |
gen().gi_code.co_consts |
| 190 |
|
| 191 |
|
| 192 |
gen.gi_code.co_consts |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
gen().send.__call__ |
| 197 |
|
| 198 |
|
| 199 |
gen().send() |
| 200 |
|
| 201 |
|
| 202 |
gen()() |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
def x(): |
| 209 |
yield |
| 210 |
|
| 211 |
|
| 212 |
next(x()) |
| 213 |
|
| 214 |
x() |
| 215 |
|
| 216 |
def x(): |
| 217 |
for i in range(3): |
| 218 |
yield |
| 219 |
|
| 220 |
|
| 221 |
next(x()) |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
def x(): |
| 228 |
a= [(yield 1)] |
| 229 |
|
| 230 |
|
| 231 |
next(x()) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def x(): |
| 237 |
foo = yield |
| 238 |
|
| 239 |
foo |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
def yield_from(): |
| 246 |
yield from iter([1]) |
| 247 |
|
| 248 |
|
| 249 |
next(yield_from()) |
| 250 |
|
| 251 |
def yield_from_multiple(): |
| 252 |
yield from iter([1]) |
| 253 |
yield str() |
| 254 |
return 2.0 |
| 255 |
|
| 256 |
x, y = yield_from_multiple() |
| 257 |
|
| 258 |
x |
| 259 |
|
| 260 |
y |
| 261 |
|
| 262 |
def test_nested(): |
| 263 |
x = yield from yield_from_multiple() |
| 264 |
|
| 265 |
x |
| 266 |
yield x |
| 267 |
|
| 268 |
x, y, z = test_nested() |
| 269 |
|
| 270 |
x |
| 271 |
|
| 272 |
y |
| 273 |
|
| 274 |
|
| 275 |
z |
| 276 |
|
| 277 |
|
| 278 |
def test_in_brackets(): |
| 279 |
x = 1 + (yield from yield_from_multiple()) |
| 280 |
|
| 281 |
x |
| 282 |
|
| 283 |
generator = (1 for 1 in [1]) |
| 284 |
x = yield from generator |
| 285 |
|
| 286 |
x |
| 287 |
x = yield from 1 |
| 288 |
|
| 289 |
x |
| 290 |
x = yield from [1] |
| 291 |
|
| 292 |
x |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
from typing import Iterator |
| 300 |
|
| 301 |
def annotation1() -> float: |
| 302 |
yield 1 |
| 303 |
|
| 304 |
def annotation2() -> Iterator[float]: |
| 305 |
yield 1 |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
next(annotation1()) |
| 310 |
|
| 311 |
next(annotation2()) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
annotation1() |