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

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

by 루시아누 하말류, 강권학
November 2024
Beginner to intermediate
1064 pages
25h 21m
Korean
Hanbit Media, Inc.
Content preview from 전문가를 위한 파이썬(2판)
730
4
제어 흐름
send( )
메서드 외에도 ‘
PEP
342
–향상된 제너레이터를 통한 코루틴 (
https://fpy.li/
pep342
)’은 코루틴이 값을 반환하는 방법을 소개했다. 다음 절에서 알아보자.
17
.
13
.
2
코루틴에서 값 반환하기
여기서는 평균을 계산하는 또 다른 코루틴을 살펴본다. 이 버전은 중간 결과를 생성하지 않는
다. 대신 항목 개수와 평균을 담은 튜플을 반환한다. 코드는 [예제
17
-
40
]과 [예제
17
-
41
]의
두 부분으로 나누었다.
예제
17-40
coroaverager2
.
py
: 파일 앞부분
from collections.abc import Generator
from typing import Union, NamedTuple
class Result(NamedTuple): ❶
count: int # type: ignore ❷
average: float
class Sentinel: ❸
def __repr__(self):
return f’<Sentinel>’
STOP = Sentinel() ❹
SendType = Union[float, Sentinel] ❺
❶ [예제
17
-
41
]의
averager2
코루틴은
Result
형의 인스턴스를 반환한다.
Result
는 사실
tuple
의 서브클래스로,
count
( )
메서드가 있지만 우리에겐
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