ROKO
[Python] Container, Sequence 본문
728x90
Container
python object는 다양한 class를 상속받을 수 있다. 그 중 data type과 무관하게 데이터를 받을 수 있는 자료구조 형을 container라고 한다.
예를들어 dict, set, str 등등은 container이고, 반대로 float, int, complex는 type이 정해진 literal 형태이다.
container는 type()으로는 확인할 수 없고 container를 상속받았는지 확인하기 위한 issubclass()를 통해 알 수 있다.
from collections import Container
print(issubclass(dict, Container))
# True
print(issubclass(int, Container))
# False
container는 method로 해당 값을 가지고 있는지 여부를 확인할 수 있다. '__contains__()'로 확인이 가능하고 'in' keyword로도 확인이 가능하다.
print('a' in 'abc')
# True
import operator
print(operator.contains('abc','a'))
# True
Sequence
container 객체중 순서를 가지고 있는 객체를 sequence라고 한다.
from collections import Container, Sequence
print(issubclass(Sequence, Container))
# True
\(sequence \in container\)
sequence는 순서가 있기 때문에 가능한 특징이 있다.
- indexing
- slicing
- +/* operation
- index search
container와 sequence모두 iterable하다. 반복가능한 객체로 반복문에 사용이 가능하다는 뜻이다.
Container의 목적은 우리는 프로그래밍을 하며 필요에 따라 single value에 여러 종류의 객체를 저장하고 싶을때가 있는데, 그것을 가능하게 해주는 것이 container이다.
https://www.analyticsvidhya.com/blog/2021/06/datatypes-and-containers-in-python-a-complete-guide/
728x90
'Computer Science > Programming Language' 카테고리의 다른 글
[Python] function (0) | 2023.03.04 |
---|---|
[Python] mutable vs immutable (0) | 2023.03.03 |
[Python] Recursion limitataion (0) | 2023.03.03 |
[Python] continue / pass / break (0) | 2023.03.02 |
Comments