You want to check the grammar and functions affected by the version, such as the trinomial operator from 2.5, or the missing xrange, in 3.x.
I wrote the code with the intention of raising an exception by checking the version (2.4 or less) before using the trigonometric operator while executing the code.
The code works well with 2.5 and higher, Below, you can't even run it in the first place. Could you tell me how to solve it?
import sys
if sys.version_info < (2, 4):
Rise "Must be greater than 2.5!"
else:
# Error in 2.4, from 2.5 to ok
x = 1 if True else 2
print x
$ ~/bin/python2.4 tern.py
File "tern.py", line 5
x = 1 if True else 2
^
SyntaxError: invalid syntax
$ ~/bin/python2.4 tern.py
Must be greater than 2.5!
In this case, it is more convenient to check directly using val rather than checking the version.
It's also inefficient to remember when functions such as with statements and trigonometric operators were added
try:
eval("1 if True else 2")
except SyntaxError:
#doesn't have ternary
I think you probably know, but if you use the __future__
module, you can use the new features in the previous version.(with etc.)
© 2024 OneMinuteCode. All rights reserved.