|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07" |
| 4 | + "signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
43 | 43 | "- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n", |
44 | 44 | "- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n", |
45 | 45 | "- [Picking True values from and and or expressions](#false_true_expressions)\n", |
46 | | - "- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)" |
| 46 | + "- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n", |
| 47 | + "- [Be aware of the consuming generator](#consuming_generator)" |
47 | 48 | ] |
48 | 49 | }, |
49 | 50 | { |
|
466 | 467 | ], |
467 | 468 | "prompt_number": 1 |
468 | 469 | }, |
| 470 | + { |
| 471 | + "cell_type": "markdown", |
| 472 | + "metadata": {}, |
| 473 | + "source": [ |
| 474 | + "<br>\n", |
| 475 | + "<br>\n", |
| 476 | + "<a name='consuming_generators'></a>\n", |
| 477 | + "\n", |
| 478 | + "## Be aware of the consuming generator\n", |
| 479 | + "\n", |
| 480 | + "Be aware using `in` checks with generators, since they won't evaluate from the beginning once a position is \"consumed\"." |
| 481 | + ] |
| 482 | + }, |
| 483 | + { |
| 484 | + "cell_type": "code", |
| 485 | + "collapsed": false, |
| 486 | + "input": [ |
| 487 | + "gen = (i for i in range(5))\n", |
| 488 | + "print('2 in gen,', 2 in gen)\n", |
| 489 | + "print('3 in gen,', 3 in gen)\n", |
| 490 | + "print('1 in gen,', 1 in gen) " |
| 491 | + ], |
| 492 | + "language": "python", |
| 493 | + "metadata": {}, |
| 494 | + "outputs": [ |
| 495 | + { |
| 496 | + "output_type": "stream", |
| 497 | + "stream": "stdout", |
| 498 | + "text": [ |
| 499 | + "2 in gen, True\n", |
| 500 | + "3 in gen, True\n", |
| 501 | + "1 in gen, False\n" |
| 502 | + ] |
| 503 | + } |
| 504 | + ], |
| 505 | + "prompt_number": 9 |
| 506 | + }, |
| 507 | + { |
| 508 | + "cell_type": "markdown", |
| 509 | + "metadata": {}, |
| 510 | + "source": [ |
| 511 | + "**We can circumvent this problem by using a simple list, though:**" |
| 512 | + ] |
| 513 | + }, |
| 514 | + { |
| 515 | + "cell_type": "code", |
| 516 | + "collapsed": false, |
| 517 | + "input": [ |
| 518 | + "l = [i for i in range(5)]\n", |
| 519 | + "print('2 in l,', 2 in l)\n", |
| 520 | + "print('3 in l,', 3 in l)\n", |
| 521 | + "print('1 in l,', 1 in l) " |
| 522 | + ], |
| 523 | + "language": "python", |
| 524 | + "metadata": {}, |
| 525 | + "outputs": [ |
| 526 | + { |
| 527 | + "output_type": "stream", |
| 528 | + "stream": "stdout", |
| 529 | + "text": [ |
| 530 | + "2 in l, True\n", |
| 531 | + "3 in l, True\n", |
| 532 | + "1 in l, True\n" |
| 533 | + ] |
| 534 | + } |
| 535 | + ], |
| 536 | + "prompt_number": 10 |
| 537 | + }, |
469 | 538 | { |
470 | 539 | "cell_type": "code", |
471 | 540 | "collapsed": false, |
|
0 commit comments