The part about other language specifications of tips you should know when programming competition with Python2 has been divided.
The Python version is ** 2.7.5 ** (Python3 has very different specifications such as input / output, so it is recommended to refer to other articles).
Python bite: Access to global variables | Inside ASCADE
Assign to a global variable in a function
Global variables are sometimes used when writing a full search, but in Python, there are points to be aware of when accessing global variables from within a function.
#If there is only a variable reference in the function
a = 'abc'
def function():
print a
function() # 'abc'
#Including reassignment to a variable within a function
a = 'abc'
def function():
a = 'def'
print a
function() # def
print a # abc
As in the above example, you can refer to a global variable from within a function, but if you assign to a local variable with the same name within that function, it will be treated as a local scope within that function. Therefore, the value of the global variable cannot be changed in the function as it is.
a = 'abc'
def function():
global a
a = 'def'
print a
function() # def
print a # def
By declaring that the variable a is a global variable by global a
, the global variable can be rewritten in the function.
Recommended Posts