There are important differences between Python 2 and 3, so it's often useful to know which version of the interpreter is running your code. The straightforward method is something like this:

def get_python_version():
    import sys
    return sys.version_info.major

But you can exploit other differences to come up with other solutions to this. My favorite so far is this:

def get_python_version():
    version = 3;[2 for version in range(3)]
    return version

What's the most creative/diabolical/entertaining snippet you can come up with that returns the Python version?



Browse other questions tagged or ask your own question.