[Tutor] Question about why a list variable is apparently global.

boB Stepp robertvstepp at gmail.com
Thu Nov 27 05:18:55 CET 2014


On Wed, Nov 26, 2014 at 6:20 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Nov 26, 2014 at 05:23:40PM -0600, boB Stepp wrote:
[...]
>> First question: How can the printLabel() function see the list
>> variable, l, defined outside of this function? I thought that
>> functions only had access to variables local to the function and
>> whatever else is passed to it during the function call.
>
> 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.
>
> Note that this principle is critical to Python, otherwise functions
> couldn't call each other, or even built-ins! If you have two functions:
>
> def f(): return g()
>
> def g(): return "Hello!"
>
> "g" is a global "variable" and f() can see it, otherwise it couldn't
> call it.

So any variables lower in the program are accessible to those above it?

> I put variable in scare-quotes because it's actually more of a
> pseudo-constant, a constant by convention, since it is very unusual to
> change things you define as a function. To be more precise, the *name*
> "g" which refers to the function exists in the module's global scope. As
> a short-cut, we call such names "global variables", even if they never
> vary.
>
> You only need to declare a global variable inside a function if you are
> re-assigning a value to it. Hence:
>
> a = 1
> b = 2
> def test():
>     global b
>     b = b + a

So even though test() has access to a and b, it won't change b when
called unless b is declared global inside the function?

> [...]
>> Third: I am always open to stylistic comments and how to be more pythonic!
>
> Fix your variable names!

I knew I would be dinged on this, but I wanted to get my main question
(about global/local) out there before I left work. The code I posted
has been changed numerous times as I dickered around with Tkinter all
day today. Despite having Grayson's book on Tkinter, it has been hard
going for me. I PROMISE crummy variable names will not make it into
the final code. Once I get the logic working and the appearance like I
want it and more importantly understand what is going on, I am going
to rewrite everything from scratch.

> You have a variable called "buttonNumber" which holds a LIST, not a
> number. Confusing!

In an earlier version buttonNumber was a number.

> You have another variable called "1", or is that "I", no sorry it's "l".
> Notice that depending on the font you use, 1 I and l can look almost
> exactly the same. That's a bad thing. Also, the name "l" doesn't mean
> anything, it doesn't tell you the purpose of the variable.

Originally l (ell) was a physical list written out. I am not used to
the way python iterates, so I was playing around with this feature in
different ways until I was sure I saw what it was doing, especially
with the 2-tuples as list elements. Now that I see how this seems to
work, I am planning on having a configuration file of some sort that
will populate this list. Where I work we have multiple centers and
people will have different versions of this list. So I intend to
separate this out and each center can have their own custom
radiobutton list to choose from.

> Then there is another variable called "var". That's nice, we already
> know it's a variable, but what role does it play in your program? Try to
> find a more descriptive name.

I have been switching back and forth between different python books.
The name var came from Grayson's generic examples. I also got caught
out by "str" as a variable name on an earlier question. That
particular book is especially bad in variable names. But again,
instead of renaming other people's code snippets, I tend to use them
as is while I am experimenting with what their snippets are trying to
do.I suppose I should be more diligent even during my "play" time in
case I wind up copying and pasting portions of the play code into code
I intend to keep. I imagine I might not remember to change all of the
awful names into good ones.

Anyway thanks for your insights!

-- 
boB


More information about the Tutor mailing list