making python 2.3 not complain about generator expressions?

D

Dan Christensen

I'd like to be able to do something like:

if pythonversion >= 2.4:
g = (x for x in [1,2,3])
else:
g = iter([1,2,3])

But even though the first branch doesn't execute in python 2.3, the
parser still complains.

I can think of some tricks using strings and eval. Is there anything
more elegant?

(By the way, what's the canonical way for a script to find out which
version of python it is running in?)

Dan
 
A

Andrew Koenig

Dan Christensen said:
I'd like to be able to do something like:

if pythonversion >= 2.4:
g = (x for x in [1,2,3])
else:
g = iter([1,2,3])

But even though the first branch doesn't execute in python 2.3, the
parser still complains.

How about

if pythonversion >= 2.4:
import newcode as code
else:
import oldcode as code
 
A

Andrew Bennetts

]
(By the way, what's the canonical way for a script to find out which
version of python it is running in?)

See sys.version, sys.version_info and sys.hexversion.

-Andrew.
 
P

Peter Hansen

Dan said:
(By the way, what's the canonical way for a script to find out which
version of python it is running in?)

Expanding on Andrew Bennetts' answer, you typically do things like
this:

if sys.version_info[:2] >= (2, 2):
print 'has 2.2 features in place'
elif sys.version_info[:3] == (1, 5, 2):
print 'precisely version 1.5.2'

etc...

That is, use slices of version_info as desired, comparing with
constant tuples.

-Peter
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top