기술(Tech, IT)/파이썬(Python)

[Python] pickle

Daniel803 2023. 12. 28. 23:36

파이썬 공식 웹사이트에서 설명하고 있는 pickle은 아래와 같다.

: pickle 모듈은 파이썬 객체 구조를 직렬화 및 역직렬화하기 위한 바이너리 프로토콜을 구현한다. "Pickling"은 파이썬 객체 계층 구조를 바이트 스트림으로 변환하는 프로세스이며, "Unpickling"은 그 반대의 작업으로, 바이너리 파일 또는 바이트-열 객체에서 바이스트 스트림을 객체 계층 구조로 다시 변환하는 작업이다.

: The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

 

내가 처음 pickle을 사용해본 적은 Machine Learning 과제 중 UC Irvine에서 제공하는 데이터 파일을 활용하라는 조건에서 였다. 이해를 돕기 위해, pickle을 비유하자면, 당신이 과일 샐러드를 보존하고 싶어 진공 포장을 한다면, 이는 "pickling"으로 비유할 수 있고, 먹기 위해 포장을 푸는 것을 "unpickling"이라고 할 수 있다.

 

조금 더 정석적인 표현으로 Pickle의 사용 목적은 아래와 같다.

  1. 파이썬 객체 보존
    : 객체를 파일이나 데이터베이스의 추후 사용을 위해 저장해두는 것
  2. 파이썬 객체 전송
    : 파이썬 객체를 다른 파이썬 프로세스나 네트워크로 전송하는 것

 

참고

- https://docs.python.org/3/library/pickle.html

- https://korbillgates.tistory.com/173

- https://wikidocs.net/110788

- https://hbase.tistory.com/401#google_vignette