[Tutor] generators

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] generators

mike jackson
I am trying understand python and have done fairly well, So for it has been easy to learn and is concise.  However I seem to not quite understand the use of a generator over a function(I am familiar with functions [other languages and math]).  To me (excepting obvious syntax differences) a generator is a function.  Why should I use a generator instead of a function or vice versa?  Is perhaps specfic uses it was created to handle?  A great web page with good examples would be nice.  Of course if you can sum it up rather easy then by all means go ahead.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] generators

Abhishek Pratap
Hey Mike

The following link should help you. http://www.dabeaz.com/generators/
. Cool slide deck with examples from David Beazley's  explanation of
generators.


-A




On Tue, Apr 3, 2012 at 11:38 AM, mike jackson <[hidden email]> wrote:
> I am trying understand python and have done fairly well, So for it has been easy to learn and is concise.  However I seem to not quite understand the use of a generator over a function(I am familiar with functions [other languages and math]).  To me (excepting obvious syntax differences) a generator is a function.  Why should I use a generator instead of a function or vice versa?  Is perhaps specfic uses it was created to handle?  A great web page with good examples would be nice.  Of course if you can sum it up rather easy then by all means go ahead.
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] generators

Joel Goldstick-2
In reply to this post by mike jackson
On Tue, Apr 3, 2012 at 2:38 PM, mike jackson <[hidden email]> wrote:
> I am trying understand python and have done fairly well, So for it has been easy to learn and is concise.  However I seem to not quite understand the use of a generator over a function(I am familiar with functions [other languages and math]).  To me (excepting obvious syntax differences) a generator is a function.  Why should I use a generator instead of a function or vice versa?  Is perhaps specfic uses it was created to handle?  A great web page with good examples would be nice.  Of course if you can sum it up rather easy then by all means go ahead.
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

A generator function is a special kind of function that uses the
'yield' statement to return a value.  The next time the function is
called, it starts up from the place following the yield statement.
They are useful in producing the next value in a computed sequence of
values without having to compute the whole sequence at one go.

Here is a great tutorial about things you can do with generators:
http://www.dabeaz.com/generators/

Here is some simple code with results below

#! /usr/bin/env python
""" generator vs normal function"""
""" a 'Normal' function"""
def n(r):
    v = []
    for i in range(r):
      v.append(i*2)
    return v

""" A generator function"""
def g(r):
    for i in range(r):
        yield i*2

print n(3)

for i in g(3):
    print i

generated_list = [i for i in g(3)]
print generated_list

[0, 2, 4]
0
2
4
[0, 2, 4]

--
Joel Goldstick
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] generators

wesley chun
> On Tue, Apr 3, 2012 at 2:38 PM, mike jackson <[hidden email]> wrote:
> I am trying understand python and have done fairly well, So for it has been easy to learn and is concise.  However I seem to not quite understand the use of a generator over a function(I am familiar with functions [other languages and math]).  To me (excepting obvious syntax differences) a generator is a function.  Why should I use a generator instead of a function or vice versa?  Is perhaps specfic uses it was created to handle?  A great web page with good examples would be nice.  Of course if you can sum it up rather easy then by all means go ahead.


dave beazley's lectures are *awesome*, and even more so if you can
attend them in-person. below are my comments on generators off the top
of my head:

1. syntactically, generators are merely functions with one or more
"yield" statements/expressions

2. users of generators will see them primarily as "advanced"
iterators, because they yield individual values until such an iterator
has been exhausted (StopIteration).

3. creators of generators will see them more like functions that you
can pause, "return" some intermediate value, then be resumable/resumed
later. the C language has the concept of a "static function" where
variables can maintain their values across function calls. while being
"nice," it's not nearly as powerful as being able to save the values
*and* the entire state of execution at the time the function is paused
then resume right where it left off later, hence the comparisons with
co-routines (which are even more independent threads of execution).

4. "generator expressions" are the lazy evaluation form of list
comprehensions, and better for memory because of that. they'll behave
just like generators but can be defined easily on a single line, just
like normal listcomps.

5. i made a quick 5-minute video introducing Python developers to
generators... it's a very ad hoc and informal session during one of my
Python courses that a student recorded. if interested in viewing it,
you can find it half-way down http://cyberwebconsulting.com

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun : wescpy at gmail : @wescpy/+wescpy
    Python training & consulting : http://CyberwebConsulting.com
    "Core Python" books : http://CorePython.com
    Python blog: http://wescpy.blogspot.com
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] generators

Bob Gailer
In reply to this post by Joel Goldstick-2
To Joel's and Wesley's valuable comments I add:

Calling a generator function returns a generator object.

>>> def x(n):
...  for i in range(n): yield i
...
>>> y = x(3)
>>> print y
<generator object x at 0x01333BE8>

A generator object can be used instead of some other "iterable" (e.g.) in for statements.
>>> for i in y:print i
0
1
2

x in this case is equivalent to xrange() with exactly 1 argument.

There is more that can be said regarding x.next(). x.send(), raise StopIteration but I've said enough for now.
-- 
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor