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

Daniel Dittmar daniel.dittmar at sap.com
Mon Apr 22 11:51:04 EDT 2002


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.
You might add a module __future__.py with a variable 'generators', but this
works only in 1.5 and 2.0, not in 2.1 (SyntaxError: future feature
generators is not defined).

Another possibility would be to have a custom importer who knows how to
parse top level if:
if PythonVersion >= 2.2:
    def generator ():
        ... yield (expr, ...) ...
else:
    class generator:
        ...

One difficulty is that you would have to cache the byte code for each Python
version:
mod.15.pyc
mod.20.pyc
...

Daniel






More information about the Python-list mailing list