ABOUT ME

  • Python data built-in classes
    파이썬/Etc. 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

    1. immutable은 변경 불가능한 객체이다.
    2. immutable 객체에는 bool,int,float,tuple,str,frozen set이 있다.
    3. 객체를 참조하는 식별자를 다른 value로 다시 할당한다면 그 인스턴스의 id는 달라진다.

    mutable

    1. mutable은 별경 가능한 객체이다.
    2. mutable객체에는 list, dict, set이 있다.
    3. 객페를 참조하는 식별자를 다른 value로 변경할수 있다. (id가 변하지 않음)

    set VS frozenset

    1. set은 가변형이고 해시값이 따로 존재하지 않는다.
    2. frozend 불변형이고 해시값이 존재한다 (해시 가능)

    Hashable?

    1. 절대 변하지 않는 해시값을 가지고 있는 경우 그 객체는 hashable하다고 말할 수 있다.(__hash__ 메서드 사용)
    2. 다른 객체와 비교할 수 있다.(__eq__ 메서드 사용)
    3. Hashability는 이러한 데이터 구조가 내부적으로 해시값을 사용하기 때문에 딕셔너리 키 또는 set의 멤버로 사용할 수 있다.

    '파이썬 > Etc.' 카테고리의 다른 글

    댓글