|
Python generator functions, lambdas, and iterators
Python provides a number of useful "extras" on functions and methods beyond what's basically needed in a cruder language. But some of the names / facilities can confuse. Here's some clarification
Lambdas
A Lambda is a function written in a single line in Python - rather like an anonymous function in Perl
Generators
A Generator is a function which doesn't do all of its work at once. Rather, it works until it has worked out part of what it has to return which it then passes back through the yield keyword ... and it then resumes when required to work out the next return value
Iterators
An iterator is returned by a generator function - in other works, it's Python's mechanism through which a fucntion can be used to "drip feed" the results back to a calling program rather that have it fed back all at once.
 You would use a generator to produce an iterator if you had a huge amount of data to handle. If you're looking for a simile (or is that a metafore?), you could compare a regular function call like asking for a bucket of water (and that will quickly get very heavy) and you could compare a generator function to being rather like having a tap on the end of a pipe which you can turn on and off and get as much water as you need (no real limit) all in manageable quantities.
You'll find some code examples linked from our Functions and Modules in Python training section. We have Python courses available too. (written 2005-04-28 08:31:53)
Associated topics are indexed under Y105 - Python - Functions, Modules and Packages
Some other Articles
Sharing pictures of WiltshireA reminder that the customer is KingUsing a Python dictionary as a holder of object attributesPricing strategy - simple and fairPython generator functions, lambdas, and iteratorsCourse follow-upsElegant languages - Perl, PHP, PythonWhy are we no. 404Object Orientation in Tcl - [incr-Tcl]MP for Devizes, Wiltshire
|
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|