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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:56:14+00:00 2026-06-03T00:56:14+00:00

Here is my class that I need to pickle: class sdict(dict): def __getattr__(self, attr):

  • 0

Here is my class that I need to pickle:

class sdict(dict):
    def __getattr__(self, attr):
        return self.get(attr, None)
    __setattr__= dict.__setitem__
    __delattr__= dict.__delitem__
    __reduce__ = dict.__reduce__

It seems to me that __reduce__ should take care of pickling, but instead:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-28-1cc94b920737> in <module>()
----> 1 pickle.dumps(sdict(a=1))

/usr/lib/python2.6/pickle.pyc in dumps(obj, protocol)
   1364 def dumps(obj, protocol=None):
   1365     file = StringIO()
-> 1366     Pickler(file, protocol).dump(obj)
   1367     return file.getvalue()
   1368 

/usr/lib/python2.6/pickle.pyc in dump(self, obj)
    222         if self.proto >= 2:
    223             self.write(PROTO + chr(self.proto))
--> 224         self.save(obj)
    225         self.write(STOP)
    226 

/usr/lib/python2.6/pickle.pyc in save(self, obj)
    304             reduce = getattr(obj, "__reduce_ex__", None)
    305             if reduce:
--> 306                 rv = reduce(self.proto)
    307             else:
    308                 reduce = getattr(obj, "__reduce__", None)

/usr/lib/python2.6/copy_reg.pyc in _reduce_ex(self, proto)
     82             dict = None
     83     else:
---> 84         dict = getstate()
     85     if dict:
     86         return _reconstructor, args, dict

TypeError: 'NoneType' object is not callable

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (171, 0))

Should I define __getstate__ too? I’m totally lost with all these multiple methods that can be used for custom pickling: __getinitargs__, __getstate__, __reduce__, etc…

Google only refers me to the official pickle documentation that I find very unclear. Can someone clarify that for me or point me to a good pickle tutorial?

  • 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-03T00:56:16+00:00Added an answer on June 3, 2026 at 12:56 am

    I question whether you are headed down a good path.

    But, to fix the pickle problem:

    class sdict(dict):
        def __getattr__(self, attr):
            if attr.startswith('__'):
                raise AttributeError
            return self.get(attr, None)
        __setattr__= dict.__setitem__
        __delattr__= dict.__delitem__
    

    Pickle is trying to get state from your object, and dict doesn’t implement it. Your __getattr__ is being called with "__getstate__" to find that magic method, and returning None instead of raising AttributeError as it should. If you fix your __getattr__, pickle works properly.

    In fact, why are you returning None for missing attributes anyway? Is that really what you want your object to do?

    This is an example of the difficulty of faking builtins as you are trying to do. There is subtle stuff going on under the covers, and you will have to be involved with it.

    PS: an identical question.

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

Sidebar

Related Questions

Here is my custom class that I have that represents a triangle. I'm trying
Here's the situation: I've got a class that is doing too much. It's mainly
I call addNotify() method in class that I posted here. The problem is, that
Here's the scenario: I'd like to have a host class that can have a
I have a class that basically stores files in amazon s3. Here is what
Here's what I am looking to accomplish, I have a class that has an
i'm missing something fundamental here. i have a very simple custom class that draws
NEWBIE ALERT! Here's the situation. I've got an Android ListActivity class (AppWindow) that contains
My motivation for chaining my class constructors here is so that I have a
I have a class that extends a class that I need to overide, but

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.