Testing for version to use from __future__ in 2.1

Fredrik Lundh fredrik at pythonware.com
Sat May 12 12:46:57 EDT 2001


Emile van Sebille wrote:
> I think the point is that nested scopes can (potentially) break existing
> code.  Existing applications should be tested to see if they break.  To do
> so, you add from __future__ import nested_scopes to your code base.

no, all you have to do is to run the code under 2.1 as is.  if it
might break, the 2.1 compiler will warn you.  consider this code:

    bar = "global"

    def spam():
        bar = "local"
        def egg():
            return bar
        return egg()

    print spam()

under Python 2.0, this prints "global"

under Python 2.1, it says "SyntaxWarning: local name 'bar' in 'spam'
shadows use of 'bar' as global in nested scope 'egg'" and then prints
"global"

under Python 2.2, and under 2.1 with a future statement, it prints
"local"

Cheers /F





More information about the Python-list mailing list