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

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

by 루시아누 하말류, 강권학
November 2024
Beginner to intermediate
1064 pages
25h 21m
Korean
Hanbit Media, Inc.
Content preview from 전문가를 위한 파이썬(2판)
223
5
데이터 클래스 빌더
5
.
3
고전적인 명명된 튜플
명명된 튜플 (
collections.namedtuple
)은 필드명, 클래스명 및 보기 좋게 출력해 주는
__
repr__( )
으로 개선된
tuple
서브클래스를 만드는 팩토리이다.
namedtuple
로 만들어진 클래
스는 튜플이 필요한 곳이면 어디든 사용할 수 있다. 실제로 튜플을 반환하는 데 쓰는 파이썬 표
준 라이브러리의 여러 함수는 이제 편의상 명명된 튜플을 반환한다. 사용자 코드는 예전 그대로 사
용할 수 있다.
TIP
namedtuple
로 생성된 클래스의 인스턴스가 사용하는 메모리양은 튜플과 똑같다. 필드명이 클래스에
저장되기 때문이다.
[예제
5
-
4
]는 도시의 정보를 담은 명명된 튜플을 정의하는 방법을 보여 준다.
예제
5-4
명명된 튜플을 사용해 정의하기
>>> from collections import namedtuple
>>> City = namedtuple(‘City’, ‘name country population coordinates’)
>>> tokyo = City(‘Tokyo’, ‘JP’, 36.933, (35.689722, 139.691667))
>>> tokyo
City(name=’Tokyo’, country=’JP’, population=36.933, coordinates=(35.689722, ...
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