파이썬/Etc.
Python data built-in classes
wookkl
2020. 9. 27. 21:53
파이썬 Data Built-in Classes
우선 클래스 들이 mutable(변)인지 immutable(불변)인지 알 필요가 있다. 예로 Float class는 immutable
이다. 인스턴스가 생성되면 그 값은 바꿀수 없다.(그 객체를 참조하는 식별자를 다른 value로 다시 할당 할수는 있지만)
일반적으로 사용되는 빌드인 클래스의 테이블이다.
Class | Description | Immutable? |
---|---|---|
bool |
Boolean value | Yes |
int |
integer | Yes |
float |
floating-point number | Yes |
list |
mutable sequence of objects | No |
tuple |
immutable sequence of objects | Yes |
str |
character string | Yes |
set |
unordered set of distinct objects | No |
frozenset |
immutable form of set class | Yes |
dict |
associative mapping | No |
immutable mutable 차이
immutable
- immutable은 변경 불가능한 객체이다.
- immutable 객체에는 bool,int,float,tuple,str,frozen set이 있다.
- 객체를 참조하는 식별자를 다른 value로 다시 할당한다면 그 인스턴스의 id는 달라진다.
mutable
- mutable은 별경 가능한 객체이다.
- mutable객체에는 list, dict, set이 있다.
- 객페를 참조하는 식별자를 다른 value로 변경할수 있다. (id가 변하지 않음)
set VS frozenset
- set은 가변형이고 해시값이 따로 존재하지 않는다.
- frozend 불변형이고 해시값이 존재한다 (해시 가능)
Hashable?
- 절대 변하지 않는 해시값을 가지고 있는 경우 그 객체는 hashable하다고 말할 수 있다.(
__hash__
메서드 사용) - 다른 객체와 비교할 수 있다.(
__eq__
메서드 사용) - Hashability는 이러한 데이터 구조가 내부적으로 해시값을 사용하기 때문에 딕셔너리 키 또는 set의 멤버로 사용할 수 있다.