Keeping Up with Python: the 2.2 Release
Generators extend from the idea of iterators. The main motivation for generators, however, comes from a different angle: they allow saving state across function calls. Static variables, such as in C functions, have the ability to maintain their value across multiple calls of that function. This partially solves the state problem, but what would be really nice would be to yield a value just like an iterator but be able to freeze execution only to resume exactly where you left off when it is called again. This is exactly what generators do. They represent the idea of merging iteration with state along with functions that are resumable. When they do, they pick up right where they left off, keeping intact all the state information they need to deliver the next item. Note that we use the term yield here for two reasons: to hint that it's not a true return (along with frame object stack pop) and to introduce the new keyword yield.
For backward compatibility (in case there is code out there that uses yield as an identifier), you must include the “from __future__ import generators” directive to use generators. Generators will become standard soon (2.3?) so importing will not be a necessity. Generators behave in another manner similar to iterators: when a real return or end-of-function is reached and there are no more values to yield, a StopIteration exception is raised. Here's a simple example:
def simpleGen(): yield 1 yield '2 --> punch!'
Now that we have our function, let's call it and get a generator object:
>>> myG = simpleGen() >>> myG.next() 1 >>> myG.next() '2 --> punch!' >>> myG.next() Traceback (most recent call last): File "", line 1, in ? myG.next() StopIterationor more aptly: for eachItem in simpleGen(): print eachItem. Of course that was a silly example. I mean, why not use a real iterator for that? More motivation comes from being able to iterate through a sequence that requires the power of a function, rather than static objects already sitting in some sequence.
In the following example, we are going to create a random iterator that takes a sequence and returns a random item from that sequence:
from random import randint def randIter(seq): while len(seq) > 0: yield seq.pop(randint(0, len(seq)-1))
The difference is that each item returned is also consumed from that sequence, sort of like a combination of list.pop() and random.choice():
>>> for eachItem in randIter([123, 'xyz', 45.678, 9j]): ... print eachItem ... 'xyz' 9j 45.678 123Table 1 is a summary of the differences between iterators and generators. You can find more details on both iterators and generators in their respective PEPs (234 and 255).
This is perhaps the most controversial update to Python so far. There are many pros and cons, but finally those who believe in true division have won out. To highlight this change let's define (or redefine) some division terms and their functionality with integer and floating-point operands.
When presented with integer operands, classic division truncates the decimal place, returning an integer (see the “Floor Division” section below). When given a pair of floating-point operands, it returns the actual floating-point quotient (see the “True Division” section). Here is an example of what Python's division has been and still is today (actually a mix of true and floor division):
>>> 1 / 2 # perform integer result (floor) 0 >>> 1.0 / 2.0 # returns real quotient 0.5
This is the case where the result should always be the actual quotient, regardless of the type of the operands. This is the big change that is to come our way when Python 3.0 nears reality. For now, to take advantage of true division, one must give the from __future__ import division directive. Once that happens, the division operator ( / ) performs only true division:
>>> from __future__ import division >>> >>> 1 / 2 # returns real quotient 0.5 >>> 1.0 / 2.0 # returns real quotient 0.5
|SUSE Linux Enterprise High Availability Extension||Mar 29, 2017|
|smbclient Security for Windows Printing and File Transfer||Mar 28, 2017|
|How to Calculate Flash Storage TCO||Mar 27, 2017|
|Non-Linux FOSS: Don't Drink the Apple Kool-Aid; Brew Your Own!||Mar 27, 2017|
|Three EU Industries That Need HPC Now||Mar 25, 2017|
|HOSTING Monitoring Insights||Mar 24, 2017|
- smbclient Security for Windows Printing and File Transfer
- Returning Values from Bash Functions
- Non-Linux FOSS: Don't Drink the Apple Kool-Aid; Brew Your Own!
- How to Calculate Flash Storage TCO
- Preseeding Full Disk Encryption
- Two Ways GDPR Will Change Your Data Storage Solution
- GRUB Boot from ISO
- Hodge Podge
- Chemistry on the Desktop
- Tech Tip: Really Simple HTTP Server with Python