Hello, "((k2,k1) for k1 in xrange(2,4) for k2 in xrange(1,k1) if gcd(k1,k2)==1)" is a "generator expression" that returns an iterator. Using list comprehension on the iterator (i.e. "[t for t in test]") advances it until it is exhausted, after which the list comprehension returns all the results.
Perhaps this will explain better: >>> foo = (x for x in range(0,10)) >>> foo <generator object <genexpr> at 0x7fd53e505cd0> >>> foo.next() 0 >>> foo.next() 1 >>> [t for t in foo] [2, 3, 4, 5, 6, 7, 8, 9] >>> foo.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> On Sat, Oct 23, 2010 at 15:27, Rolandb <[email protected]> wrote: > Hi, look at the following simple routine. > > def why(): > > test=((k2,k1) for k1 in xrange(2,4) for k2 in xrange(1,k1) if > gcd(k1,k2)==1) > > print [t for t in test] > print [t for t in test] > > return > > why() > [(1, 2), (1, 3), (2, 3)] > [] > > It seems that "test" can only be used once. > > The Python documentation does not indicate a loss of a 'generator' > once used. > > Question: Python or Sage behavior? > > Roland > > -- > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/sage-support > URL: http://www.sagemath.org > -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
