Skip to Content
전문가를 위한 파이썬(2판)
book

전문가를 위한 파이썬(2판)

by 루시아누 하말류, 강권학
November 2024
Beginner to intermediate
1064 pages
25h 21m
Korean
Hanbit Media, Inc.
Content preview from 전문가를 위한 파이썬(2판)
414
2
객체로서의 함수
10
.
2
.
2
함수 지향 전략
[예제
10
-
1
]에서 각각의 구체적인 전략은
discount( )
라는 메서드 하나가 있는 클래스다. 게
다가 전략 객체에는 상태 (객체 속성)가 없다. 구체적인 전략 객체가 일반 함수처럼 보인다면,
제대로 본 것이다. [예제
10
-
3
]은 [예제
10
-
1
]을 리팩터링한 코드로, 구체적인 전략을 간단히
함수로 변경하고
Promotion
추상 클래스를 제거했다.
3
예제
10-3
할인 전략을 함수로 구현한
Order
클래스
from collections.abc import Sequence
from dataclasses import dataclass
from decimal import Decimal
from typing import Optional, Callable, NamedTuple
class Customer(NamedTuple):
name: str
fidelity: int
class LineItem(NamedTuple):
product: str
quantity: int
price: Decimal
def total(self):
return self.price * self.quantity
@dataclass(frozen=True)
class Order: # 콘텍스트
customer: Customer
cart: Sequence[LineItem] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

전문가를 위한 리액트

전문가를 위한 리액트

테자스 쿠마르
고성능 파이썬(2판)

고성능 파이썬(2판)

오현석, 미샤 고렐릭, 이안 오스발트

Publisher Resources

ISBN: 9791169211772