By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Writing backwards compatible code

P: n/a
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.

My first approach was to use a try...except block to test for generator
expressions:

try:
gen = (something for x in blah)
except SyntaxError:
def g():
for x in blah:
yield something
gen = g()
This failed to work under 2.3, because the SyntaxError occurs at compile
time, and so the try block never happens.

I've been burnt by making assumptions before, so I tried a second,
similar, approach:

if sys.version_info >= (2, 4):
gen = (something for x in blah)
else:
# you know the rest

As expected, that failed too.

The solution which worked was to put the generator expression in a second
module, then import that:

try:
import othermodule
except SyntaxError:
# fall back code
What techniques do others use?
--
Steven.

Apr 14 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.
Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
not necessary at all. If you are going to have the ugly, syntactically bitter
version in your code anyways, why clutter up your code even more trying to do both?
What techniques do others use?


Personally, I write code that only uses the syntactic features of the Python
version that I am targetting.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 14 '06 #2

P: n/a
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
What techniques do others use?


I'd just write the generator with a yield statement. The generator
expression does the same thing more concisely, I think.
Apr 14 '06 #3

P: n/a
Em Sex, 2006-04-14 *s 13:28 -0500, Robert Kern escreveu:
Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.


Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
not necessary at all. If you are going to have the ugly, syntactically bitter
version in your code anyways, why clutter up your code even more trying to do both?


Right. You can always use classes.

--
Felipe.

Apr 14 '06 #4

P: n/a
Felipe Almeida Lessa wrote:
Em Sex, 2006-04-14 *s 13:28 -0500, Robert Kern escreveu:
Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.


Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
not necessary at all. If you are going to have the ugly, syntactically bitter
version in your code anyways, why clutter up your code even more trying to do both?


Right. You can always use classes.


Well, I guess you could, but using actual generators would be much cleaner.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 14 '06 #5

P: n/a
Em Sex, 2006-04-14 *s 13:37 -0500, Robert Kern escreveu:
Felipe Almeida Lessa wrote:
Em Sex, 2006-04-14 *s 13:28 -0500, Robert Kern escreveu:
Steven D'Aprano wrote:

I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.

Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
not necessary at all. If you are going to have the ugly, syntactically bitter
version in your code anyways, why clutter up your code even more trying to do both?


Right. You can always use classes.


Well, I guess you could, but using actual generators would be much cleaner.


That's the whole point around syntactic sugar: being cleaner, more
concise, (sometimes) less error-prone and (less times) faster.

--
Felipe.

Apr 14 '06 #6

P: n/a
On Sat, Apr 15, 2006 at 04:04:44AM +1000, Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.

My first approach was to use a try...except block to test for generator
expressions:

try:
gen = (something for x in blah)
except SyntaxError:
def g():
for x in blah:
yield something
gen = g()

The solution which worked was to put the generator expression in a second
module, then import that:

try:
import othermodule
except SyntaxError:
# fall back code

What techniques do others use?


If you want to support 2.3 just use the yield version. Having
two versions of the same thing is asking for trouble. Twice
the tests, twice the room for breakage, twice the places to
update if you want to change it.

-Jack

Apr 14 '06 #7

P: n/a
Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible.

My first approach was to use a try...except block to test for generator
expressions:

try:
gen = (something for x in blah)
except SyntaxError:
def g():
for x in blah:
yield something
gen = g()
This failed to work under 2.3, because the SyntaxError occurs at compile
time, and so the try block never happens.

I've been burnt by making assumptions before, so I tried a second,
similar, approach:

if sys.version_info >= (2, 4):
gen = (something for x in blah)
else:
# you know the rest

As expected, that failed too.

The solution which worked was to put the generator expression in a second
module, then import that:

try:
import othermodule
except SyntaxError:
# fall back code
What techniques do others use?


Here is one every one will have fun lambasting:

try:
exec('gen = (something for x in blah)')
except:
def g():
# etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Apr 15 '06 #8

P: n/a
Steven D'Aprano wrote:
I came across an interesting (as in the Chinese curse) problem today. I
had to modify a piece of code using generator expressions written with
Python 2.4 in mind to run under version 2.3, but I wanted the code to
continue to use the generator expression if possible. [snip] What techniques do others use?


About the only reliable one I know of is to not use syntax from the newer
version, period, if the code needs to run on old versions. Your example was
pretty straightforward, but unless the use case is always that simple it's too
easy to have two separate versions of the code to maintain, and there's no way
to enforce that changes/fixes to one will be duplicated in the other.

So... if you have to take the time to make one that works with the older
version, you might as well just use it exclusively.

-Dave
Apr 16 '06 #9

P: n/a
Is there a list of all of the Python commands and modules that tell when
(what version) they were added to Python? I was hoping the new Essential
Reference would have it, but it doesn't.

Thanks!

Bob
Apr 18 '06 #10

P: n/a
Bob Greschke wrote:
Is there a list of all of the Python commands and modules that tell when
(what version) they were added to Python? I was hoping the new Essential
Reference would have it, but it doesn't.


Here's a reference that stops at 2.3:

http://rgruet.free.fr/PQR2.3.html

--Scott David Daniels
sc***********@acm.org
Apr 18 '06 #11

P: n/a
On Tue, 18 Apr 2006 13:40:11 -0600,
"Bob Greschke" <bo*@greschke.com> wrote:
Is there a list of all of the Python commands and modules that tell
when (what version) they were added to Python? I was hoping the new
Essential Reference would have it, but it doesn't.


I thought it was more cohesive, but googling for

python "what's new"

turns up a collection of such documents in various places.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Apr 19 '06 #12

P: n/a
On 19/04/2006 5:40 AM, Bob Greschke wrote:
Is there a list of all of the Python commands and modules that tell when
(what version) they were added to Python? I was hoping the new Essential
Reference would have it, but it doesn't.


Possibly because it was deemed to be not essential :-)

For (a) writing from scratch a module that's targeted at a particular
version of Python or (b) back-porting an existing module to a particular
version, you need the docs for that version. As a reminder of features
not to use in case (a) or that need changes in case (b), it's very handy
to have a list of syntax and module changes made after that version ...
for this I can't imagine anything better than amk's excellent "What's
New in Python x.y" series.

Apr 19 '06 #13

P: n/a
> Bob Greschke wrote:
Is there a list of all of the Python commands and modules that tell when
(what version) they were added to Python? I was hoping the new Essential
Reference would have it, but it doesn't.


The Library Reference page for a module or built-in often documents the
version when any change was made. You can also read through the What's
New documents for each release to see what changed in that release.

Kent
Apr 19 '06 #14

P: n/a
Scott David Daniels a crit :
Bob Greschke wrote:
Is there a list of all of the Python commands and modules that tell
when (what version) they were added to Python? I was hoping the new
Essential Reference would have it, but it doesn't.


Here's a reference that stops at 2.3:

http://rgruet.free.fr/PQR2.3.html


Remove the 2.3, and you get the 2.4 :-)

http://rgruet.free.fr/
A+

Laurent.
Apr 24 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.