[Tutor] Question about why a list variable is apparently global.
Steven D'Aprano
steve at pearwood.info
Thu Nov 27 16:37:48 CET 2014
On Thu, Nov 27, 2014 at 09:13:35AM -0600, boB Stepp wrote:
> On Thu, Nov 27, 2014 at 4:56 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > On Wed, Nov 26, 2014 at 10:18:55PM -0600, boB Stepp wrote:
> >
> >> So any variables lower in the program are accessible to those above it?
> >
> > No, that can't be the explanation. Think of this:
> >
> > b = a + 1
> > a = 2
> >
> > That will fail because when the "b = a + 1" line is executed, a doesn't
> > exist yet so there is no way to get a value for it.
>
> This was my original understanding! I see now that I totally
> misunderstood what you said in your earlier post:
>
> "No, they also have access to globals and built-ins. You define the list
> l at the top level of your module. That makes it a global, so the
> printLavel() function can see it."
>
> I did not understand what you meant by "top level of your module".
I hope I have been more clear now, but just in case, "top level" means
code not inside a class or function. It's not quite the same as indent
levels, since not all indents create a new scope, but similar.
# module
a
b
def func():
c
d
e
f
a, b, e and f are "top level" (module level), c and d are in func().
--
Steven
More information about the Tutor
mailing list