Laziness and the infinite

Generators are lazy, meaning that computation only happens just-in-time when the next result is requested. This interesting and useful property of generators means they can be used to model infinite sequences. Since values are only produced as requested by the caller, and since no data structure needs to be built to contain the elements of the sequence, generators can safely be used to produce never-ending (or just very large) sequences like:

  • Sensor readings
  • Mathematical sequences (Example: primes, factorials, and so on.)
  • The contents of a multi-terabyte file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset