Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (I am trying to call the global copy of a variable created in a separate function.)

x = "somevalue"

def func_A ():

global x

# Do things to x

return x

def func_B():


# Do things

return x



Does the 'x' that the second function uses have the same value of the global copy of 'x' that 'func_a' uses and modifies? When calling the functions after definition, does order matter?

1 Answer

0 votes
by (106k points)

The global variables can be accessed by its name only. But to change its value you need to use the global keyword.


global abc

abc = 55

The above code will change the value of the global variable to 55. Otherwise, it would just assign 55 to a local variable.

Related questions

0 votes
2 answers
+1 vote
2 answers
+1 vote
2 answers
asked May 29, 2019 in R Programming by Suresh (3.4k points)
0 votes
1 answer

Browse Categories