Modules that provide differing functionality amongst different Python versions...

holger krekel pyth at devel.trillke.net
Mon Apr 22 12:33:49 EDT 2002


On Mon, Apr 22, 2002 at 05:51:04PM +0200, Daniel Dittmar wrote:
> Allan Crooks wrote:
> > The main problem is when I want to use 'yield', there's no real way of
> > compensating for a language change... I suppose it is my fault for
> > trying too much. :) The more I think about it, the more maintaining
> > concurrent versions seems appealing... at least there is no 'hacking'
> > involved...
> 
> You could do 'yield (expr, ...)', which looks like a yield to 2.2 and like a
> routine call to anyone else.
> 
> There is still the problem how to declare
> from __future__ import generators
> It has to be the very first statement, so it can't be inside a try-except.

yes. actually i looked up the exact condition up in the C-Source. 
It doesn't need to be the first statement in your *module*. It needs
to be the first statement in any *parsing tree*. So

  import os
  from __future__ import generators

does not work. but 

  import os
  exec "from __future__ import generators"

works because the exec-string part leads to a new parsing tree.
the __future__ stuff modifies what the parsers sees as 'valid' python.
that's why it has to be at the beginning of building the parsing tree.

It follows that while you can call any generator defined inside the exec-string
you cannot use "yield"-sytanx outside the exec.

	holger





More information about the Python-list mailing list