Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does 'global' behave under an if statement?

In my program, I wanted a variable global only under some circumstances. Say it looks like this:

a = 0
def aa(p):
    if p:
        global a
    a = 1
    print("inside the function " + str(a))


print(a)
aa(False)
print("outside the function " + str(a))

I was expecting the result to be:

0
inside the function 1
outside the function 0

However it turned out to be:

0
inside the function 1
outside the function 1

So, I was thinking, "Okay maybe the Python compiler makes the variable global whenever it sees the 'global' keyword no matter where it is located". Is this how Python works with global vars? Am I misunderstanding?

like image 487
INJUNE16 Avatar asked May 08 '17 03:05

INJUNE16


People also ask

Are variables in IF statements global?

Re: Using global variables in functions with if statements There are global and local variables. All variables declared in the first (global) namespace are global. All other variables (declared in a function) are local in the function.

How do you know if a function is global or local?

Find out the difference between global and local variables. Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function.

What is an if statement used for?

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.

What does a global statement do inside a function?

The global keyword is the way that you tell python that a particular variable in your function is defined at the global (module-level) scope. In other words, you can change the value of myvariable in the module-scope from within func if you use the global keyword.


1 Answers

Yes, you're understanding things correctly.

The global statement isn't something that's evaluated at runtime. It's really a directive to the parser that essentially tells it to treat all listed identifiers (a here) as referring to the global scope. From the docs on the global statement:

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.

It then continues to state how global is really is a directive:

Programmer’s note: global is a directive to the parser.

Using it conditionally doesn't make any difference: its presence has already been detected in the parsing stage and, as a result, the byte-code generated for grabbing the names has already been set to look in the global scope (with LOAD/STORE GLOBAL).

This is why, if you dis.dis a function containing a global statement, you won't see any relevant byte-code for global. Using a silly function:

from dis import dis
def foo():
    "I'm silly"
    global a  

dis(foo)
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE

Nothing is generated for global a because the information it provides has already been used!

like image 184
Dimitris Fasarakis Hilliard Avatar answered Oct 16 '22 21:10

Dimitris Fasarakis Hilliard