[Edu-sig] Re: ..., closures, ... other weirdness
Kirby Urner
urnerk at qwest.net
Tue Jan 27 11:56:30 EST 2004
> On the other hand, your example is probably simpler as a genuine,
> iterable, generator:
>
> def weird():
> x,y = 1,1
> while True:
> yield x
> x,y = y, x+y
>
> from itertools import islice
> wi = islice(weird(), 20)
> for i in wi: print i,
>
> We use islice to prevent iterating forever, as this will happily give
> you every fibonnacci number until the end of time otherwise.
>
> --Dethe
Yes, the generator syntax is clearly more straightforward than using
function attributes.
As Martelli points out in '..in a Nutshell', anything making use of function
attributes heavily is more idiomatically (and easily) implemented using
class syntax.
Thanks for showing the islice trick. Have you made a study of itertools?
I'd be interested in learning other such tips, if you've got the time.
Kirby
More information about the Edu-sig
mailing list