Skip to main content

PYTHON SCOPE

S
COPE
def myfunc():
  x = "Local Scope"
  print(x)
myfunc()
Local Scope
def myfunc():
  x = "Function Inside Function"
  def myinnerfunc():
    print(x)
  myinnerfunc()
myfunc()
Function Inside Function
x = "Global Scope"
def myfunc():
  print(x)
myfunc()
Global Scope
print(x)
Global Scope
x = "Global Scope"
def myfunc():
  x = "Local Scope"
  print(x)
myfunc()
Local Scope
print(x)
Global Scope
Global Keyword
def myfunc():
  global x
  x = 300
myfunc()
print(x)
300
x = 300
def myfunc():
  global x
  print(x)
  x = 200
  print(x)
myfunc()
300
200
print(x)
200

Comments