diff options
author | Prabhu Ramachandran | 2018-06-07 01:30:23 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2018-06-07 01:30:23 +0530 |
commit | c05978bd1d5464988300eead1c1d8af98f732a42 (patch) | |
tree | b6173edee2d8a0b66e695ef3718308403e44e875 /advanced_python/16_generators.ipyml | |
parent | 3170893245a922fce640a64f0c3bc3f6009ae1e3 (diff) | |
download | python-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.tar.gz python-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.tar.bz2 python-workshops-c05978bd1d5464988300eead1c1d8af98f732a42.zip |
Fixing some mistakes in the slides.
Diffstat (limited to 'advanced_python/16_generators.ipyml')
-rw-r--r-- | advanced_python/16_generators.ipyml | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/advanced_python/16_generators.ipyml b/advanced_python/16_generators.ipyml index b30162d..bf6e427 100644 --- a/advanced_python/16_generators.ipyml +++ b/advanced_python/16_generators.ipyml @@ -12,7 +12,6 @@ cells: slideshow: slide_type: slide - - markdown: | ## Background @@ -95,7 +94,7 @@ cells: next(c) - code: | - for i in counter(4): + for i in counter(5): print(i) metadata: @@ -150,7 +149,6 @@ cells: for i in range(n): yield 2*i + 1 - - markdown: | ## Exercise: Fibonacci generator @@ -162,7 +160,7 @@ cells: slide_type: slide - code: | - for x in fib(5): + for x in fib(10): print(x) - markdown: | @@ -176,13 +174,10 @@ cells: def fib(n): a, b = 0, 1 yield 0 - for i in range(n-1) + for i in range(n-1): yield b a, b = b, a + b - -# --------------- Part 2 -------------- - - markdown: | ## Creating iterable objects @@ -230,7 +225,21 @@ cells: self.animals = list(animals) def __iter__(self): - return self.animals + return iter(self.animals) + +- code: | + class Animal(object): + def __init__(self, name): + self.name = name + def greet(self): + print(self.name, "says hi!") + + class Mammal(Animal): + pass + + metadata: + slideshow: + slide_type: slide - code: | c = Animal('crow') @@ -247,8 +256,8 @@ cells: - markdown: | ## Observations - - Just returns `self.animals` - - `self.animals` is iterable as it is a list + - Just returns `iter(self.animals)` + - `self.animals` is a `list` and calling `iter` makes it an iterator metadata: slideshow: @@ -283,7 +292,7 @@ cells: slide_type: subslide - code: | - r = SillyRange(5) + r = SimpleRange(5) for i in r: print(i) @@ -291,7 +300,6 @@ cells: slideshow: slide_type: subslide - - markdown: | ## Observations @@ -304,7 +312,6 @@ cells: slideshow: slide_type: slide - - markdown: | ## Summary @@ -317,9 +324,8 @@ cells: slideshow: slide_type: slide - - markdown: | - ## Exercise: Fibonacci generator object + ## Exercise: Fibonacci object Create a class that when instantiated with an argument `n` can be used as a sequence of the first `n` numbers of the Fibonacci sequence, call this @@ -357,7 +363,7 @@ cells: return old - code: | - f = Fib(5) + f = Fib(10) for i in f: print(i) @@ -365,10 +371,10 @@ cells: slideshow: slide_type: slide - # The lines below here may be deleted if you do not need them. # --------------------------------------------------------------------------- metadata: + celltoolbar: Slideshow kernelspec: display_name: Python 3 language: python @@ -382,7 +388,7 @@ metadata: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.0 + version: 3.5.2 rise: scroll: true transition: none |