On Wed, 25 Jan 2006 15:50:27 -0600, [EMAIL PROTECTED] wrote:

> 
>    >> If they need to resume their calculations from where they left off
>    >> after the last yield.
>
>    Wolfgang> Well, no, independently from that.
>
>    Wolfgang> Just to avoid to inital overhead of the function call.
>
>How do you pass in parameters?  Consider:
>
>    def square(x):
>        return x*x
>
>vs
>
>    def square(x)
>        while True:
>            yield x*x
>
>How do you get another value of x into the generator?
>
>    >>> def square(x):
>    ...   while True:
>    ...     yield x*x
>    ...
>    >>> g = square(2)
>    >>> g
>    <generator object at 0x3b9d28>
>    >>> g.next()
>    4
>    >>> g.next()
>    4
>    >>> g.next(3)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>    TypeError: expected 0 arguments, got 1
>
 >>> def square(xbox):
 ...     while True: yield xbox[0]*xbox[0]
 ...
 >>> xbox = [3]
 >>> g = square(xbox)
 >>> g.next()
 9
 >>> xbox[0]=4
 >>> g.next()
 16
 >>> [g.next() for xbox[0] in xrange(10)]
 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

One way to answer your question literally,
whatever it may do to your gag reflex ;-)

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to