[PYTHON] Local scope and global scope

Understand the difference between local scope and global scope Here are some things to keep in mind when dealing with each variable.

What are local scope and global scope?

The local scope refers to the inside of the defined function, and the variables available in it are called local variables.

def sample():
    a =1 * Local scope

The global scope points to the target python file and can be referenced from any function.

b =2 * Global scope
def sample():
    a = 2

How to handle the global scope

Global scope can be handled from within local scope

local = 100
def global():
    a = 100
    print(local + a)

Recommended Posts

Local scope and global scope
Global and local variables 2
Global and local variables 1
A story about Go's global variables and scope
Differences between Ruby and Python in scope
Difference between nonlocal and global in Python