您可以使用进行测试
eval:
try: eval("1 if True else 2")except SyntaxError: # doesn't have ternary另外,
with是 可以在Python 2.5,只需添加
from __future__ import with_statement。
编辑:要尽早获得控制权,您可以将其拆分为不同的
.py文件,并在导入之前检查主文件中的兼容性(例如,
__init__.py在软件包中):
# __init__.py# Check compatibilitytry: eval("1 if True else 2")except SyntaxError: raise importError("requires ternary support")# import from another modulefrom impl import *


