Scope

การเขียนฟังก์ชันใน Python

Shayne Miel

Software Architect @ Duo Security

ชื่อ

tom

การเขียนฟังก์ชันใน Python

ชื่อ

tom and janelle

การเขียนฟังก์ชันใน Python

Scope

tom and janelle

การเขียนฟังก์ชันใน Python

Scope

tom, janelle, และ tom อีกคน

การเขียนฟังก์ชันใน Python

Scope

x = 7
y = 200
print(x)
7
def foo():
    x = 42
    print(x)
    print(y)
foo()
42
200
print(x)
7
การเขียนฟังก์ชันใน Python

Scope

คำถามเรื่อง scope

การเขียนฟังก์ชันใน Python

Scope

local scope

การเขียนฟังก์ชันใน Python

Scope

global scope

การเขียนฟังก์ชันใน Python

Scope

builtin scope

การเขียนฟังก์ชันใน Python

Scope

nonlocal scope

การเขียนฟังก์ชันใน Python

คีย์เวิร์ด 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
การเขียนฟังก์ชันใน Python

คีย์เวิร์ด 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
การเขียนฟังก์ชันใน Python

มาฝึกกันเถอะ!

การเขียนฟังก์ชันใน Python

Preparing Video For Download...