Skip to content

Commit 5f8b044

Browse files
committed
3월 28일(금) 오전수업
1 parent 521620b commit 5f8b044

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

2025-03-28.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ else:
208208
209209
---
210210
#### 식 안에서 대입을 수행할 수 있는 := 연산자
211+
* 지금까지 파이썬에서는 코드에서 표현식을 인라인(inline)으로 캡쳐하는 문법이 없었다.
212+
* expression(클로져) 클로져는 자기범위 밖의 값을 캡쳐해서 스코프 안으로 끌고 들어간다.
213+
211214
- [[Python] 유용한 새로운 연산자! 바다코끼리 연산자 := (walrus operator)](https://bio-info.tistory.com/120)
212215
#### 1. 개념
213216
```
@@ -226,6 +229,11 @@ else:
226229
<T>() 터보피쉬
227230
```
228231
232+
- [[Python] 클로저(Closure)](https://gsbang.tistory.com/entry/Python-%ED%81%B4%EB%A1%9C%EC%A0%80Closure)
233+
- [[Python] 클로저(Closure)](https://velog.io/@cataiden/python-closure)
234+
```
235+
📌 반환된 내부함수가 자신이 선언되었을 때의 환경을 기억하고, 외부함수 밖에서 호출되더라도 그 환경에 다시 접근할 수 있는 함수
236+
```
229237
230238
> ```
231239
> import random
@@ -254,10 +262,77 @@ else:
254262
255263
256264
---
265+
#### 예외 처리
266+
```
267+
if ~ elif ~ else
257268

258-
---
269+
try ~ except ~ finally
270+
271+
파이썬에서는 문제가 생기면 그때 처리하자.
272+
미리 걱정할 필요가 없다.
273+
```
274+
275+
- [실제 파이썬: 파이썬에서 오류 처리 또는 방지하기: LBYL vs EAFP](https://puddingcamp.com/topics/python-error-handling-lbyl-vs-eafp)
276+
- [[Python] 파이썬 예외 처리 방법 (try, except, else, finally)](https://maker5587.tistory.com/81)
277+
- ##### 여러 종류의 예외 처리
278+
```
279+
data_list = [10, '20', 'invalid', None, '']
280+
for value in data_list:
281+
try:
282+
     result = int(value)
283+
    except ValueError:
284+
     print(f'ValueError: {value}를 정수로 변환할 수 없습니다. 기본값 0으로 설정합니다. ')
285+
       result = 0
286+
    except TypeError:
287+
     print(f'TypeError: {value}의 타입이 잘못되었습니다. 기본값 0으로 설정합니다.')
288+
        result = 0
289+
   print(result)
290+
```
291+
292+
- #### 'else' 및 'finally' 블록 사용하기
293+
- 'else': 예외가 발생하지 않을 때만 실행됩니다.
294+
- 'finally': 예외 발생 여부와 관계없이 항상 실행됩니다.
295+
```
296+
file_path = 'example.txt'
297+
298+
try:
299+
file = open(file_path, 'w')
300+
   file.write('파이썬 예외 처리 예시')
301+
except IOError:
302+
print(f"파일 '{file_path}'을 열 수 없습니다.")
303+
else:
304+
print(f"파일 '{file_path}'에 데이터 쓰기 성공!")
305+
finally:
306+
print(f"'{file_path}' 처리가 완료되었습니다.")
307+
    file.close()
308+
```
309+
310+
311+
312+
---
313+
#### except 절 ── 예외가 발생했을 때만 실행함
314+
- [예외 계층 구조](https://docs.python.org/ko/3.13/library/exceptions.html#exception-hierarchy)
259315
260316
317+
```
318+
def get_book(index):
319+
items = ['note', 'notebook', 'sketchbook']
320+
try:
321+
return items[index]
322+
except (IndexError, TypeError) as e:
323+
print(f'예외가 발생했습니다: {e}')
324+
return '범위 밖입니다'
325+
```
326+
일반적으로 except Exception as e: 를 추천
327+
```
328+
def get_book(index):
329+
items = ['note', 'notebook', 'sketchbook']
330+
try:
331+
return items[index]
332+
except Exception as e:
333+
print(f'예외가 발생했습니다: {e}')
334+
return '범위 밖입니다'
335+
```
261336
262337
---
263338

0 commit comments

Comments
 (0)