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

Allan Crooks googlegroups at sixtyten.org
Sun Apr 21 10:29:08 EDT 2002


Hi,

I was wondering what the best approach is on providing libraries for
varying versions of Python?

My aim is to distribute a module that will provide a set of core
functions, but provide more functions to later versions of Python.

For example, I make a module that works on 2.0, but if used on 2.1,
will provide more functions to say, do with the warning framework. If
used on 2.2, certain functions that return lists in earlier versions
of Python, will use generators instead.

Rather than keeping several versions of the same module (but for use
with different versions of Python), I was wondering if there was a
more elegant solution?

I had thought initially of doing something like this:

---------------

<2.0 code>
if sys.version[:2] < (2, 1): return

<2.1 code>
if sys.version[:2] < (2, 2): return

<2.2 code>

---------------

But that wouldn't work, because earlier versions of Python would still
report SyntaxErrors, as it would have a problem with iterator
statements.

Another thought I've had is doing it by specifying, say for module
'x', having modules 'x_20', 'x_21', 'x_22', with x simply finding out
what versions of Python it is running on, and then compiling all
modules it can deal with, and then importing all objects into it's
namespace.

That idea sounds a bit 'hacky' to me, so can anyone suggest a nicer
way of doing this? There's also the problem of trying to specify class
definitions which provide more methods in later versions of Python. Do
later versions subclass earlier ones, or do they just interfere with
the namespace?

Thanks,
Allan.



More information about the Python-list mailing list