2030 Engineer
[파이썬 객체지향] LBYL 스타일과 EAFP 스타일 본문
LBYL : Look Before You Leap
뛰기 전에 살펴보라 (돌다리도 두들겨 보고 건너라)
EAFP : Easier to Ask for Forgiveness than Permission
허락보다 용서가 쉽다! (일단 먼저 빨리 실행하고 문제가 생기면 처리한다.
1
2
3
4
|
LBYL식이면 이렇게 만약 ~라면 실행하고 아니면 안된다고 출력할께라는 식이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def add_shape(self, shape: Shape):
def total_area_of_shapes(self):
total_area = 0
try:
except(AttributeError, TypeError):
print("그림판에 area 메소드가 없거나 잘못 정의되어 있는 인스턴스 {}가 있습니다.".format(shape))
return total_area
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
try부분에서 에러가 발생하면 except에서 Attribute에러, TypeError가 발생시 오류문구를 출력하고 하던 기능을 마저 실행한다. 일단 하고 문제가 발생하면 처리!
'Programming > Python' 카테고리의 다른 글
[파이썬 알고리즘] 선형탐색과 이진 탐색 / 선택정렬과 삽입정렬 (0) | 2020.02.25 |
---|---|
[파이썬 객체지향] SOLID 원칙 (0) | 2020.02.23 |
[파이썬 객체지향] 캡슐화 (0) | 2020.02.18 |
[파이썬 객체 지향] 추상 클래스 다중 상속 (0) | 2020.02.18 |
[파이썬 객체 지향] 추상클래스와 추상화 (0) | 2020.02.18 |
Comments