Python code written in 1998, how to improve/change it?

skip at pobox.com skip at pobox.com
Wed Jan 25 16:50:27 EST 2006


 
    >> 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

Skip



More information about the Python-list mailing list