Scope (Gültigkeitsbereich)

Funktionen in Python schreiben

Shayne Miel

Software Architect @ Duo Security

Namen

tom

Funktionen in Python schreiben

Namen

Tom und Janelle

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

Tom und Janelle

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

Tom, Janelle und noch ein Tom

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

x = 7
y = 200
print(x)
7
def foo():
    x = 42
    print(x)
    print(y)
foo()
42
200
print(x)
7
Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

scope_question

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

local scope

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

global scope

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

builtin scope

Funktionen in Python schreiben

Scope (Gültigkeitsbereich)

nonlocal scope

Funktionen in Python schreiben

Das Schlüsselwort „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
Funktionen in Python schreiben

Das Schlüsselwort „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
Funktionen in Python schreiben

Lass uns üben!

Funktionen in Python schreiben

Preparing Video For Download...