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

holger krekel pyth at devel.trillke.net
Mon Apr 22 11:58:58 EDT 2002


On Mon, Apr 22, 2002 at 07:33:15AM -0700, Allan Crooks wrote:
> > But if you are truly set on using incompatible constructs  
> > *everywhere* for the fun of it, you are going to have a problem
> > with code duplication :-)
> 
> I wouldn't actually mind having code which would do the following:
> 
> --------
> 
> try:
>     import warning
> except ImportError:
>     warning = None
> 
> ....
> 
> if warning is not None:
>     <do something with warning module>
> 
> --------
> 
> 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 probably refer to my posting :-)
If so, let me note that "execing a string" is what python does itself. 
e.g. when you import a module the interpreter is execing a module-string or 
code-object. It's some weird hack but the regular python way.

As long as you use your pre2.2/post2.2 "function" with
"for i in function()"  or "filter(foo,function())"
you can easily do the following:

minversion("2.2",'''

from __future__ import generators

def function():   # full generator version
	...
	yield xyz

''','''

def function():   # fully-constructed list version
	l=[]
	for item in soandso:
		...
		l.append(item)
	return l

''')

This is precisely what i want to express.

Your mileage may vary, of course.

  holger





More information about the Python-list mailing list