Python Generators
PySthlm Meetup #15 – Python
Best Practices – Oct 31, 2013
Samuel Lampa

twitter.com/smllmp
The “normal” (bad) way
The “normal” (bad) way

New temporary data structures
created and kept in memory
== BAD!!!
Using Generator function
Using Generator function

A yield (“return”) for
every iteration, so it can
be used immediately
without temp data structur...
Using Generator function

A yield (“return”) for
every iteration, so it can
be used immediately
without temp data structur...
Using Generator function

A yield (“return”) for
every iteration, so it can
be used immediately
without temp data structur...
Using list-comprehension syntax
Using list-comprehension syntax

Here, the generator function
Is defined in one row
Chaining generator functions
Chaining generator functions
Creds and more info:
●

David Beazleys Python Generator intro:

→dabeaz.com/generators
David Beazley on twitter: @dabeaz
Thank you!
Samuel Lampa
G+: gplus.to/saml
twitter: @smllmp
Blog: saml.rilspace.org
Developer @
www.uppmax.uu.se
www.farmbi...
Python Generators - Talk at PySthlm meetup #15
Upcoming SlideShare
Loading in...5
×

Python Generators - Talk at PySthlm meetup #15

472
-1

Published on

Describing how you can use python generator functions to process a file without loading it all in memory before starting

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
472
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python Generators - Talk at PySthlm meetup #15

  1. 1. Python Generators PySthlm Meetup #15 – Python Best Practices – Oct 31, 2013 Samuel Lampa twitter.com/smllmp
  2. 2. The “normal” (bad) way
  3. 3. The “normal” (bad) way New temporary data structures created and kept in memory == BAD!!!
  4. 4. Using Generator function
  5. 5. Using Generator function A yield (“return”) for every iteration, so it can be used immediately without temp data structure == GOOD!
  6. 6. Using Generator function A yield (“return”) for every iteration, so it can be used immediately without temp data structure == GOOD! So, this is the generator function
  7. 7. Using Generator function A yield (“return”) for every iteration, so it can be used immediately without temp data structure == GOOD! So, this is the generator function … which can be iterated over, like this ...
  8. 8. Using list-comprehension syntax
  9. 9. Using list-comprehension syntax Here, the generator function Is defined in one row
  10. 10. Chaining generator functions
  11. 11. Chaining generator functions
  12. 12. Creds and more info: ● David Beazleys Python Generator intro: →dabeaz.com/generators David Beazley on twitter: @dabeaz
  13. 13. Thank you! Samuel Lampa G+: gplus.to/saml twitter: @smllmp Blog: saml.rilspace.org Developer @ www.uppmax.uu.se www.farmbio.uu.se www.rilpartner.se
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×