2030 Engineer

[파이썬 객체지향] LBYL 스타일과 EAFP 스타일 본문

Programming/Python

[파이썬 객체지향] LBYL 스타일과 EAFP 스타일

Hard_Try 2020. 2. 18. 18:21

LBYL : Look Before You Leap

뛰기 전에 살펴보라 (돌다리도 두들겨 보고 건너라)

EAFP : Easier to Ask for Forgiveness than Permission

허락보다 용서가 쉽다! (일단 먼저 빨리 실행하고 문제가 생기면 처리한다.

 

1
2
3
4
if isinstance(name, Account):
else:
    print("이건 계좌에 들어가지 못하는 이름입니다.")

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
 
    for shape in self.shapes:
        try:
            total_area += shape.area()
        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가 발생시 오류문구를 출력하고 하던 기능을 마저 실행한다. 일단 하고 문제가 발생하면 처리!

Comments