scoping problems

Steven D. Majewski sdm7g at Virginia.EDU
Fri Jul 27 15:34:44 EDT 2001


On Fri, 27 Jul 2001, Bjorn Pettersen wrote:

> > From: Narayan Desai [mailto:desai at mcs.anl.gov]
> > 
> > How can i define the following code:
> > 
> > def a():
> >     b()
> > 
> > def b():
> >     a()
> > 
> 
> What's wrong with that code? (did you try it in the interpreter?)
> 
> -- bjorn
> 

I'm not sure what he's trying to do with the above code, 
but with generators in 2.2, you can do:

>>> from __future__ import generators
>>> def a():
...     yield b()
... 
>>> def b():
...     yield a()
... 
>>> a()    
<generator object at 0x9759ec>
>>> a().next
<method-wrapper object at 0x80d870>
>>> a().next()
<generator object at 0x80546c>
>>> a().next().next()
<generator object at 0x806e2c>
>>> a().next().next().next()
<generator object at 0x805d7c>
>>> a().next().next().next().next()
<generator object at 0x80548c>
>>> a().next().next().next().next().next()
<generator object at 0x805d7c>
>>> a().next().next().next().next().next().next()
<generator object at 0x80548c>
>>> a().next().next().next().next().next().next().next()
<generator object at 0x805d9c>
>>> a().next().next().next().next().next().next().next().next()
<generator object at 0x805d7c>
>>> 

I'm not sure what THAT is good for either, but at least it doesn't
stack overflow!

I keep finding more and more amazing uses for this new feature! ;-) 

-- Steve Majewski







More information about the Python-list mailing list