copyregpickle 지원 함수 등록

소스 코드: Lib/copyreg.py


The copyreg module offers a way to define functions used while pickling specific objects. The pickle and copy modules use those functions when pickling/copying those objects. The module provides configuration information about object constructors which are not classes. Such constructors may be factory functions or class instances.

copyreg.constructor(object)

object를 유효한 생성자로 선언합니다. object가 콜러블이 아니면 (따라서 생성자로 유효하지 않으면), TypeError가 발생합니다.

copyreg.pickle(type, function, constructor_ob=None)

functiontype 형의 객체에 대한 “환원” 함수로 사용되어야 한다고 선언합니다. function는 문자열이나 2개에서 또는 6개 사이의 요소를 포함하는 튜플을 반환해야 합니다. function의 인터페이스에 대한 자세한 내용은 dispatch_table을 참조하십시오.

The constructor_ob parameter is a legacy feature and is now ignored, but if passed it must be a callable.

피클러 객체나 pickle.Pickler의 서브 클래스의 dispatch_table 어트리뷰트도 환원 함수를 선언하는 데 사용될 수 있습니다.

예제

아래 예제는 피클 함수를 등록하는 방법과 사용법을 보여줍니다.:

>>> import copyreg, copy, pickle
>>> class C:
...     def __init__(self, a):
...         self.a = a
...
>>> def pickle_c(c):
...     print("pickling a C instance...")
...     return C, (c.a,)
...
>>> copyreg.pickle(C, pickle_c)
>>> c = C(1)
>>> d = copy.copy(c)
pickling a C instance...
>>> p = pickle.dumps(c)
pickling a C instance...