Wednesday, January 21, 2026
HomeLanguagescopyreg — Register pickle support functions

copyreg — Register pickle support functions

The copyreg module defines functions which are used by pickling specific objects while pickling or copying. This module provides configuration information about object constructors(may be factory functions or class instances) which are not classes.

copyreg.constructor(object)
This function is used for declaring object as a valid constructor. An object is not considered as a valid constructor if it is not callable. This function raises TypeError if the object is not callable.

copyreg.pickle(type, function, constructor=None)
This is used to declare function as a “reduction” function for objects of type type. function should return either a string or a tuple containing two or three elements.
The constructor parameter is optional. It is a callable object which can be used to reconstruct the object when called with the tuple of arguments returned by function at pickling time. TypeError is raised if object is a class or constructor is not callable.

Example :




# Python 3 program to illustrate 
# use of copyreg module 
import copyreg, copy, pickle 
  
class B(object): 
    def __init__(self, a): 
        self.a =
  
def pickle_b(b): 
    print("pickling a C instance..."
    return C, (b.a, ) 
  
copyreg.pickle(B, pickle_b) 
b = B(1
d = copy.copy(b) 
print (d) 
  
r = pickle.dumps(b) 
print (r) 


Output :

pickling a C instance...

pickling a C instance...
b'\x80\x03c__main__\nC\nq\x00K\x01\x85q\x01Rq\x02.'

This article is contributed by Aditi Gupta. If you likeneveropen and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on theneveropen main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS