scope of variables

Steve R. Hastings steve at
Wed May 3 17:37:02 EDT 2006

On Thu, 04 May 2006 07:02:43 +1000, Gary Wessle wrote:
> b = 3
> def adding(a)
>     print a + b
> it seams not to see the up-level scope where b is defined.

Assuming you put a ':' after the "def adding(a)", this should work in
recent versions of Python.  In Python 2.0 and older, this will not work.

In Python 2.1, it will only work if you do this:

from __future__ import nested_scopes

When you first start Python interactively, it should print version
information.  Here's what my Python prints when I start it:

Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

As you can see, I'm running Python 2.4.3.  Make sure you aren't running an
old version of Python, and that code should do what you expect.
Steve R. Hastings    "Vita est"
steve at

More information about the Python-list mailing list