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

Carl Banks imbosol at vt.edu
Sun Apr 21 16:19:28 EDT 2002


holger krekel wrote:
> On Sun, Apr 21, 2002 at 02:12:16PM -0400, Carl Banks wrote:
>> Allan Crooks wrote:
>> > Hi,
>> > 
>> > I was wondering what the best approach is on providing libraries for
>> > varying versions of Python?
>> > (...)
> 
>> # Import stuff for version 2.1
>> if sys.version[:2] >= (2,1):
>>     from mymodule2_1 import *
>> 
>> # Import for version 2.2
>> if sys.version[:2] >= (2,2):
>>     from mymodule2_2 import *
> 
> this solves part of the 'code duplication' problem. Might get a bit tricky
> if you just want to define a few additional methods for some classes
> (for example 'generator versions' of methods otherwise returning lists).

I wouldn't say so.  It is straightforward enough to add a method to a
class any time:

>>> class a:
...    pass
...
>>> def p(self,x):
...    print "hello, %s" % x
...
>>> a.p = p
>>> b = a()
>>> b.p("world")
hello, world



> Ah, and by the way how is
> 
>        sys.version[:2] >= (x,y) 
> 
> supposed to work? On my python 2.2 
> 
>        sys.version[:2] == '2.'   is True
>
> am i missing something here?

I just did it how the OP did it, without bothering to check.  It
should be sys.version_info[:2] >= (x,y)


-- 
CARL BANKS                                http://www.aerojockey.com
"Nullum mihi placet tamquam provocatio magna.  Hoc ex eis non est."



More information about the Python-list mailing list