Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:40:31+00:00 2026-06-15T09:40:31+00:00

I’m playing around with class inheritances and I’m stuck on how to pickle data

  • 0

I’m playing around with class inheritances and I’m stuck on how to pickle data in the class dictionary.

If dump only the dictionary part of self, when I load the dictionary back to self, self takes on a dict type instead of a class. But if I pickle the whole class then I get an error.

Error

pickle.PicklingError: Can't pickle <class 'main.model'>: it's not the same object as main.model 

Code

import os, pickle

class model(dict):
    def __init__( self ):
        pass

    def add( self, id, val ): 
        self[id] = val

    def delete( self, id ):   
        del self[id]

    def save( self ): 
        print type(self)
        pickle.dump( dict(self), open( "model.dict", "wb" ) )

    def load( self ): 
        print 'Before upacking model.dic, self ==',type(self)
        self = pickle.load( open( "model.dict", "rb" ) ) 
        print 'After upacking model.dic, self ==',type(self)

if __name__ == '__main__':
    model = model()
    #uncomment after first run
    #model.load()

    #comment after first run
    model.add( 'South Park', 'Comedy Central' )
    model.save()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:40:32+00:00Added an answer on June 15, 2026 at 9:40 am

    If all you want to do is have a class called model that is a subclass of dict and can be properly pickled and unpickled back to an object that is also of type model then you don’t have to do anything special. The methods you have defined in your example to add and delete are unnecessary you can just do them directly on model instances as you would do with any other dict. The save and load methods can be done using the pickle module instead of on the class itself.

    code

    import pickle
    
    class model(dict):
        pass
    
    a = model()
    pickled = pickle.dumps(a)
    b = pickle.loads(pickled)
    
    print type(a), a
    print type(b), b
    

    output

    <class '__main__.model'> {}
    <class '__main__.model'> {}
    

    below is another version which is maybe more in line with what you were trying to achieve. But you should NOT do things this way. The load method is weird so is the save. I put the code below to show it can be done but not really something you want to do because it will end up being very confusing.

    another version (don’t do this)

    import pickle
    
    class model(dict):
        def save(self):
            with open("model.dict", "wb") as f:
                pickle.dump(self, f)
    
        def load(self): 
            with open("model.dict") as f:
                return pickle.load(f)
    
    #comment after first run
    test = model()
    test['South Park'] = 'Comedy Central'
    test.save()
    print type(test), test
    
    #uncomment after first run
    test2 = model().load()
    print type(test2), test2
    

    Further Reading

    A great example of a subclass of dict that is is picklable is collections.OrderedDict. It is part of the python standard library and is implemented in python so you can have a peak at the source. The definition is 172 lines of code so it’s not too much code to look through. It also had to implement the __reduce__ method to achieve pickling because it has information about the order of items that also needs to be pickled and unpickled. It’s a good example of why you might want to make your own subclass of dict, it adds the very useful feature of respecting the order of values added to the dict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am currently running into a problem where an element is coming back from
I'm parsing an XML file, the creators of it stuck in a bunch social
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.