Python 1.6 The balanced language

Just van Rossum just at letterror.com
Mon Sep 4 04:30:52 EDT 2000


Tim Peters wrote:
> Guido already knows how to implement generators without Stackless, and
> indeed that's (ironically enough!) one of Stackless's problems with getting
> into the core:  the one pretty compelling use with broad appeal (iteration a
> la CLU and Sather) can be implemented straightforwardly without Stackless.
> Generators are also much more usable if a little syntax is added to support
> them (like a "suspend" stmt, as in Icon too).

I don't agree a new *statement* would make it "much more usable".
Here's your very own pi generator example (Demo/threads/Generator.py),
reworked to use my experimental stackless-based co-routine
implementation. It has a suspend() *function*:

import coro

def pi():
	k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
	while 1:
		# Next approximation
		p, q, k = k*k, 2L*k+1L, k+1L
		a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
		# Print common digits
		d, d1 = a/b, a1/b1
		while d == d1:
			coro.suspend(int(d))
			a, a1 = 10L*(a%b), 10L*(a1%b1)
			d, d1 = a/b, a1/b1

def test():
	g = coro.new(pi)
	for i in range(10):
		print g(),
	print
	g = coro.new(pi)
	while 1:
		print g(),

test()


Just



More information about the Python-list mailing list