Scope

Functies schrijven in Python

Shayne Miel

Software Architect @ Duo Security

Namen

tom

Functies schrijven in Python

Namen

tom en janelle

Functies schrijven in Python

Scope

tom en janelle

Functies schrijven in Python

Scope

tom, janelle en nog een tom

Functies schrijven in Python

Scope

x = 7
y = 200
print(x)
7
def foo():
    x = 42
    print(x)
    print(y)
foo()
42
200
print(x)
7
Functies schrijven in Python

Scope

scope_vraag

Functies schrijven in Python

Scope

lokale scope

Functies schrijven in Python

Scope

globale scope

Functies schrijven in Python

Scope

ingebouwde scope

Functies schrijven in Python

Scope

niet-lokale scope

Functies schrijven in Python

Het sleutelwoord global

x = 7

def foo():
  x = 42
  print(x)

foo()
42
print(x)
7
x = 7

def foo():
  global x
  x = 42
  print(x)

foo()
42
print(x)
42
Functies schrijven in Python

Het sleutelwoord nonlocal

def foo():
  x = 10

  def bar():
    x = 200
    print(x)

  bar()
  print(x)

foo()
200
10
def foo():
  x = 10

  def bar():
    nonlocal x
    x = 200
    print(x)

  bar()
  print(x)

foo()
200
200
Functies schrijven in Python

Laten we oefenen!

Functies schrijven in Python

Preparing Video For Download...