(python) zip, raise, assert, local, global

berry ·2022년 1월 19일
0

Python

목록 보기
13/15

📌 zip


📌 지역변수, 전역변수

전역변수(global): 파일 내에 어디서든 영향력이 있는 함수
지역변수(local): 함수 내에서만 영향력이 있는 함수

함수 안에서는 전역변수를 수정할 수 없으나,
함수 내에서

global 변수

를 선언하면 그 변수가 전역변수임을 밝히며, 함수 내에서도 global 변수를 호출하여 수정이 가능하다.

g_var = 'g_var'   # 전역변수
# 값 수정후(수정값)
def variables():
  
  global glo_var  # global 전역변수를 함수 내에서 전역변수라고 선언
  glo_var = 'glo_var' # global 전역변수에 새로운 값 할당 
  lo_var = 'lo_var'   # 지역변수

📌 예외처리

에러가 발생했음을 알려주고 프로그램을 종료시키는 함수

1_ raise

raise Exception(문구)
raise ValueError(문구)

a = '1'
if type(a) != 'int':
    raise Exception('a must be int type') # raise Exception

2 _ assert

unittest 처럼 시험이나 디버깅을 위한 목적성이 강함

assert 조건, '문구'

a = int(input())
assert type(a) != int, 'a must be int type`

3_ try - except - finally


Reference

profile
Engineer

0개의 댓글