Périmètre

Écrire des fonctions en Python

Shayne Miel

Software Architect @ Duo Security

Noms

tom

Écrire des fonctions en Python

Noms

tom and janelle

Écrire des fonctions en Python

Périmètre

tom and janelle

Écrire des fonctions en Python

Périmètre

tom, janelle, and another tom

Écrire des fonctions en Python

Périmètre

x = 7
y = 200
print(x)
7
def foo():
    x = 42
    print(x)
    print(y)
foo()
42
200
print(x)
7
Écrire des fonctions en Python

Périmètre

scope_question

Écrire des fonctions en Python

Périmètre

local scope

Écrire des fonctions en Python

Périmètre

global scope

Écrire des fonctions en Python

Périmètre

builtin scope

Écrire des fonctions en Python

Périmètre

nonlocal scope

Écrire des fonctions en Python

Le mot-clé 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
Écrire des fonctions en Python

Le mot-clé non local

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
Écrire des fonctions en Python

Passons à la pratique !

Écrire des fonctions en Python

Preparing Video For Download...