Ritz
Ritz

Reputation: 1253

How do I set a global variable via function call?

I am trying to change the value of global variable edl_loading to True in function edl_flashing ,somehow it doesn't work?can anyone help understand why does print edl_loading prints False after call to edl_flashing in which I change the value to True

def edl_flashing():
    edl_loading = True
    print edl_loading


def main ():
    global edl_loading
    edl_loading = False
    print edl_loading
    edl_flashing()
    print edl_loading #Why this prints as False
if __name__ == '__main__':
    main()

OUTPUT:-

False
True
False

Upvotes: 1

Views: 38

Answers (1)

Andy
Andy

Reputation: 50630

You need to use the global in both of your functions - main and edl_flashing

def edl_flashing():
    global edl_loading
    edl_loading = True
    print edl_loading

Without the global declaration in the function, the variable name is local to the function.

The above change prints out

False
True
True

Upvotes: 1

Related Questions