Writing backwards compatible code

S

Steven D'Aprano

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?
 
R

Robert Kern

Steven said:
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
(e-mail address removed)

"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
 
D

Dave Brueck

Steven said:
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
 
F

Felipe Almeida Lessa

Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
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.
 
P

Paul Rubin

Steven D'Aprano said:
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.
 
R

Robert Kern

Felipe said:
Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:


Right. You can always use classes.

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

--
Robert Kern
(e-mail address removed)

"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
 
F

Felipe Almeida Lessa

Em Sex, 2006-04-14 às 13:37 -0500, Robert Kern escreveu:
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.
 
J

Jack Diederich

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
 
J

James Stroud

Steven said:
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/
 
B

Bob Greschke

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
 
S

Scott David Daniels

Bob said:
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
(e-mail address removed)
 
D

Dan Sommers

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
 
J

John Machin

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.
 
K

Kent Johnson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top