My Generator Paradox!

Schüle Daniel uval at rz.uni-karlsruhe.de
Thu Mar 16 20:39:11 EST 2006


it's easy to explain

class X:
	pass

x=X()
y=X()

x and y are different instances
one can put in x
x.item = 1
y doesn't even have an attribute item for example

similar with generators
they are *different* objects of same kind generator

 >>> def fib():
...     a,b = 1,1
...     while True:
...             a,b = b,a+b
...             yield a,b
...
 >>> f1 = fib()
 >>> f2 = fib()
 >>> f1
<generator object at 0x4042866c>
 >>> f2
<generator object at 0x404db42c>	# different addresses
 >>> f1 is f2
False
 >>> f1.next()
(1, 2)
 >>> f1.next()
(2, 3)
 >>> f1.next()
(3, 5)
 >>>
 >>>
 >>> f2.next()
(1, 2)
 >>>

it's only natural that each objects starts it's own fibonaci serie

hth, Daniel




More information about the Python-list mailing list