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
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- The Italian Army Switches to LibreOffice
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Linux Mint 18
- Oracle vs. Google: Round 2
- Varnish Software's Varnish Massive Storage Engine
- The FBI and the Mozilla Foundation Lock Horns over Known Security Hole
- Devuan Beta Release
- Privacy and the New Math
- Ben Rady's Serverless Single Page Apps (The Pragmatic Programmers)
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide