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

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

by 루시아누 하말류, 강권학
November 2024
Beginner to intermediate
1064 pages
25h 21m
Korean
Hanbit Media, Inc.
Content preview from 전문가를 위한 파이썬(2판)
470
3
클래스와 프로토콜
이제
Vector
클래스 안에 시퀀스 프로토콜을 구현하자. 여기에서는 슬라이싱을 지원하지 않지
만, 나중에 슬라이싱 지원 기능을 추가한다.
12
.
5
Vector
버전 #
2
: 슬라이스 가능한 시퀀스
FrenchDeck
예제에서
self._components
를 사용했듯이, 객체 안에 들어 있는 시퀀스 속성에
위임하면 시퀀스 프로토콜을 구현하기 위한
__len__( )
__getitem__( )
메서드를 다음과
같이 아주 쉽게 구현할 수 있다.
class Vector:
# 중략
# ...
def __len__(self):
return len(self._components)
def __getitem__(self, index):
return self._components[index]
이 두 메서드가 추가되었으니, 다음과 같은 연산을 수행할 수 있다.
>>> v1 = Vector([3, 4, 5])
>>> len(v1)
3
>>> v1[0], v1[-1]
(3.0, 5.0)
>>> v7 = Vector(range(7))
>>> v7[1:4]
array(‘d’, [1.0, 2.0, 3.0])
보다시피 (아주 잘되지는 않지만 ) 슬라이싱도 지원된다.
Vector
의 슬라이스도 배열이 아니
Vector
객체가 되면 더 좋을 것이다. 이전의
FrenchDeck ...
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