On Monday, August 22, 2016 at 1:36:07 AM UTC-4, Steven D'Aprano wrote: > On Monday 22 August 2016 14:33, Chris Angelico wrote: > > > On Mon, Aug 22, 2016 at 1:37 PM, rocky <ro...@gnu.org> wrote: > >> Sorry should have been: > >> > >> assert sys.version_info >= (3,0) > > > > The next question is: How common is code like this? I don't put > > version checks in any of my modules. Adding magic comments would be of > > value only if this sort of thing is common enough to need its own > > syntax. > > Most of my code is supposed to work on Python 2.4 through the latest 3.x > version. That means I have lots of version-specific code. > > But most of it uses feature detection rather than version checks. So instead > of > writing: > > if sys.version < '3': > def next(iterator): > return iterator.next() > > > I write this: > > try: > next > except NameError: > def next(iterator): > return iterator.next() > > or possibly this: > > > if not hasattr(builtins, 'next'): > def next(iterator): > return iterator.next() > > > But some features depend on new syntax, and you can't check for the > availability of new syntax at runtime. (Well, you can, by clever use of > exec(), > but its too much trouble than its worth.) So very, very occasionally I have > to > do a version check: > > > if sys.version < '3': > import mymodule2 as mymodule > else: > import mymodule3 as mymodule > > > But... I don't understand what this proposal actually is. We already have a > uniform way to indicate the Python language version: check sys.version, or > sys.version_info, or sys.hexversion, whichever is more appropriate for your > needs.
The fact that you mention 3 different ways says to me this isn't uniform. > Because this is just an ordinary comparison, you can then do whatever > you like, no matter how outlandish or unusual: > > > if sys.version < '3': > try: > import mymodule3 > except SyntaxError: > print("Yay, a syntax error!") > else: > print("Now that's weird, that shouldn't happen...") > del mymodule3 > import mymodule2 as mymodule > elif config[RUN_OLD_VERSION]: > import mymodule2 as mymodule > else: > import mymodule3 as mymodule > > > Could somebody (the OP?) please explain what is the purpose of this proposal, > what it does, how it works, and when would people use it? All of the ways you mention involve running the program. Suppose you have a tool that analyzes source code. It reads the code, but doesn't run it. For example possibly code climate. How it it figure out which dialect of Python is being used? > > > > -- > Steve -- https://mail.python.org/mailman/listinfo/python-list