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

  • Home
  • SEARCH
  • 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 6122607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:55:08+00:00 2026-05-23T15:55:08+00:00

What is the best way to write a __getstate__ method that pickles almost all

  • 0

What is the best way to write a __getstate__ method that pickles almost all of an object’s attributes, but excludes a few?

I have an object with many properties, including one that references an instancemethod. instancemethod’s are not pickleable, so I’m getting an error when I try to pickle this object:

class Foo(object):
    def __init__(self):
        self.a = 'spam'
        self.b = 'eggs'
        self.c = 42
        self.fn = self.my_func
    def my_func(self):
        print 'My hovercraft is full of eels'

import pickle
pickle.dumps(Foo())              # throws a "can't pickle instancemethod objects" TypeError

This __getstate__ method fixes this, but then I have to manually include all the properties I want to serialize:

def __getstate__(self):
    return { 'a': self.a, 'b': self.b, 'c': self.c }

That’s not very scalable or maintainable if I have an object with many attributes or that changes frequently.

The only alternative I can think of is some kind of helper function that iterates through an object’s properties and adds them (or not) to the dictionary, based on the type.

  • 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-23T15:55:09+00:00Added an answer on May 23, 2026 at 3:55 pm

    The only alternative I can think of is some kind of helper function that iterates through an object’s properties and adds them (or not) to the dictionary, based on the type.

    Yeah, I think that’s pretty much what you’re left with, if you want enough "magic" to allow yourself to be lazy (and/or allow for dynamically added attributes). Keep in mind that "pickle can’t handle this" isn’t the only reason you might not want to include something in the pickled state.

    But it’s not as hard as you seem to think, assuming you have code for the "should I pickle this?" logic:

    def __getstate__(self):
      return {k:v for (k, v) in self.__dict__.items() if should_pickle(v)}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to write a thread safe method? I have the
What's the best way to write a LINQ query that inserts a record and
What is the best way to write a function (or something DSLish) that will
What would be the best way to define a method in the model that
Trying to find the best way to write this SQL statement. I have a
I'm wondering: what is the best way to write a Clojure program that interacts
I'm looking for the best way to write a function that takes a string
I recently read (and unfortunately forgot where), that the best way to write operator=
what is the best way to write a query that combines AND , OR
What is the best way to write a class based view that both shows

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.