On Mon, Feb 16, 2015 at 7:20 PM, Cameron Simpson <c...@zip.com.au> wrote: > One might use exec() to use code that is valid in one python version but not > another, when you need your program to run in both i.e. to get code that is > syntacticly invalid in one version, but to use it (conditionally) in another > version. > > I only have one use case for this presently: I have a use of exec() in my > cs.py3 python2/3 compatability module: > > def raise3(exc_type, exc_value, exc_traceback): > if sys.hexversion >= 0x03000000: > raise exc_type(exc_value).with_traceback(exc_traceback) > else: > # subterfuge to let this pass a python3 parser; ugly > exec('raise exc_type, exc_value, exc_traceback') > > I'm using exec() here because a Python 3 interpreter will reject the 3 > argument form of raise. Elsewhere in my code I just call cs.py3.raise3() > with the requisite arguments. Note that the string passed to exec() is > hardwired, not obtained from elsewhere in any form.
I'd try conditional imports, first: if sys.hexversion >= ...: from .compat_py3 import raise3 else: from .compat_py2 import raise3 But maybe this breaks with the setuptools pre-compilation shenanigans? At any rate, I didn't mean to make a general statement. Obviously, sometimes exec/eval is necessary. If for no other reason than because sometimes the requirement is to use exec (e.g. if you are implementing something equivalent to python -i, etc.). -- Devin _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor