Sunday, February 28, 2010

Global Variables

If you want to access same variable in multiple functions, you declare it as global var1 in your program.

Global variables can be read from all the functions but if you need to modify them, you need to declare them again in that function. Why? So that you don't inadvertently cause cascading effect. It's a preventive measure.

But then how are we able to read the variables w/o declaring them? It's a friendly feature. Python looks for local variables and if they are not found; it looks for any global variable. Since you're just trying to read the variable; it won't (hopefully) be a costly mistake.

No comments:

Post a Comment