티스토리 뷰
함수 내부의 이름공간 : 지역 영역(Local scope)
함수 밖의 영역 : 전역 영역(Global scope)
파이썬 자체에서 정의한 영역 : 내장 영역(Built-in scope)
>>> x =1
>>> def func(a):
return a + x #Local scope에 해당 이름이 없음, Global scope에서 같은 이름을 찾아서 사용
>>> func(1)
2
>>> def func2(a):
x = 2 #Local scope에 x라는 이름 등록
return a + x #Local scope에 x를 사용
>>> func2(1)
3
-. 이름검색규칙(‘LGB 규칙’)
Local -> Global -> Built-in 의 순서대로 검색
-. Local에서 Global 변수 사용방법
>>> g = 1
>>> def ts(a):
global g
g = 2
return g + a
>>> ts(1)
3
-. global의 사용방법
>>> del g
>>> def tg(a):
global g
return g + a
>>> tg(1)
Traceback (most recent call last):
File "<pyshell#128>", line 1, in <module>
tg(1)
File "<pyshell#127>", line 3, in tg
return g + a
NameError: global name 'g' is not defined
>>> g = 2
>>> tg(1)
3
global은 Global의 변수를 Local로 참조만 시킴(Loal에서 Global 변수생성 불가)
Global에 해당 변수가 없으면 오류 발생, 해당 변수를 Global에서 선언해주면 해결
'Python' 카테고리의 다른 글
Python, 함수 lambda (0) | 2016.04.21 |
---|---|
Python, 함수_인수 (0) | 2016.04.21 |
Python, 함수_인수 전달 (0) | 2016.04.21 |
Python, 함수 (0) | 2016.04.21 |
Python, 복사 (0) | 2016.04.21 |
- Total
- Today
- Yesterday
- vba
- activeforeground
- ManyToOne
- JPA
- tkinter
- Module
- 폼
- Private
- command
- Excel
- Java
- 파이썬
- 리눅스
- fetch join
- onetomany
- tkinter command & bind [명령어묶기와 사건묶기] Python
- activebackground
- Python
- disabledforeground
- borderwidth
- apache
- 상수
- checkbutton
- IdClass
- highlightthickness
- Composite Key
- FetchType
- indicatoron
- Linux
- highlightbackground
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |