On 27/11/14 04:18, boB Stepp wrote:

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?

No.
Its not whether they are defined above or below each other its the level of indentation. Both f and g are defined at the outer level of the module. They are therefore global(within that module) and can each see the other. Steven could just as well have written:

def f(): return "Hello!"

def g(): return f()

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?

Correct, if it tries to, it will create a new local variable
b inside test(). In the absence of a global declaration Python
uses initial assignment as name creation.

day today. Despite having Grayson's book on Tkinter, it has been hard
going for me.

Grayson's book is hard going and is very old now so some things have improved and changed. In particular you now have access to Tix and ttf on newer versions of Python. But since you are still using 2.4 that probably doesn't affect you! :-)

However, if you have access to it, you should consider getting Mark Lutz's book Programming Python (4th ed). It's a massive 1600 pages
of which 400+ are about Tkinter programming (more than Grayson!)

And there is the bonus of 1200 other pages of detailed info about
the OS, Networking, databases etc. Again its a bit dry but full
of information.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to