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 7533493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:44:32+00:00 2026-05-30T05:44:32+00:00

New to python… I have the following class Key, that extends dict: class Key(

  • 0

New to python…
I have the following class Key, that extends dict:

class Key( dict ):

    def __init__( self ):
        self = { some dictionary stuff... }

    def __getstate__(self):
        state = self.__dict__.copy()
        return state

    def __setstate__(self, state):
        self.__dict__.update( state )

I want to save an instance of the class with its data using pickle.dump and then retrieve the data using pickle.load. I understand that I am supposed to somehow change the getstate and the setstate, however, am not entirely clear on how I am supposed to do that… any help would be greatly appreciated!

  • 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-05-30T05:44:34+00:00Added an answer on May 30, 2026 at 5:44 am

    I wrote a subclass of dict that does this here it is.

    class AttrDict(dict):
        """A dictionary with attribute-style access. It maps attribute access to
        the real dictionary.  """
        def __init__(self, *args, **kwargs):
            dict.__init__(self, *args, **kwargs)
    
        def __getstate__(self):
            return self.__dict__.items()
    
        def __setstate__(self, items):
            for key, val in items:
                self.__dict__[key] = val
    
        def __repr__(self):
            return "%s(%s)" % (self.__class__.__name__, dict.__repr__(self))
    
        def __setitem__(self, key, value):
            return super(AttrDict, self).__setitem__(key, value)
    
        def __getitem__(self, name):
            return super(AttrDict, self).__getitem__(name)
    
        def __delitem__(self, name):
            return super(AttrDict, self).__delitem__(name)
    
        __getattr__ = __getitem__
        __setattr__ = __setitem__
    
        def copy(self):
            return AttrDict(self)
    

    It basically converts the state to a basic tuple, and takes that back again to unpickle.

    But be aware that you have to have to original source file available to unpickle. The pickling does not actually save the class itself, only the instance state. Python will need the original class definition to re-create from.

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

Sidebar

Related Questions

New to Python. I'm on Windows and Python2.7. I have confirmed that my directory
I'm a new Python programmer who is having a little trouble using 'self' in
Im new two python and am trying to grow a dictionary of dictionaries. I
I am new to Python, and I'm working on writing some database code using
I am really new to Python and I have been looking for an example
I'm new to python, and have a list of longs which I want to
hey guys, I need help considering win32com in Python: I have a routine that
Suppose a programmer has the following problem: he wants to start a new python
Relatively new to python. I recently posted a question in regards to validating that
Im new to python programming and need some help with some basic file I/O

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.