파이썬[Python]: keyword - nonlocal 키워드

내장함수 - nonlocal 키워드(keyword)


/// 설명

중첩함수(nested function)에서 상위 함수의 변수 내용을 변경(encapsulated code) 할 수 있습니다.

※ 형식
nonlocal

reference
https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-nonlocal_stmt

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def func_a():
    a = "a_string"
    def func_b():
        a = "func_a_string"
    print(a)  # a_string
    func_b()
    print(a)  # a_string
 
 
func_a()
 
def func_c():
    b = "b_string"
    def func_d():
        nonlocal b
        b = "func_b_string"
    print(b)  # b_string
    func_d()
    print(b)  # func_b_string
 
 
func_c()
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수