V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Grenadn
V2EX  ›  Python

Python2 这种情况下如何做版本检查?(语法不兼容)

  •  
  •   Grenadn · Dec 21, 2015 · 3185 views
    This topic created in 3791 days ago, the information mentioned may be changed or developed.

    含有如下的代码:

    def foo(a, *, k=v):
        pass
    

    在 python2 运行时会因为星号出现语法错误,所以没法运行版本检查代码

    if sys.version_info < (3, 0):
        sys.stderr.write('ERROR: Python 3.0 or newer version is required.\n')
        sys.exit(1)
    

    thus ……
    没有办法提示版本错误?

    5 replies    2015-12-30 18:59:33 +08:00
    sivacohan
        1
    sivacohan  
    PRO
       Dec 21, 2015 via Android
    把这个检查放在文件的最上面。
    yelite
        2
    yelite  
       Dec 21, 2015
    分两个文件,检查完了再 import
    Grenadn
        3
    Grenadn  
    OP
       Dec 21, 2015
    @sivacohan 解释器错误不给运行放哪也没用
    @yelite 只能这样了把,好麻烦
    pc10201
        4
    pc10201  
       Dec 22, 2015
    应该像下面这样写吧?
    *或**应该放到最后面
    def foo(a, k=v, *):
    pass
    qnnnnez
        5
    qnnnnez  
       Dec 30, 2015 via iPhone
    可以把 def 放到 if 里面
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3145 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    ♥ Do have faith in what you're doing.