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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:21:37+00:00 2026-06-18T06:21:37+00:00

I’m trying to use pickle to save a custom class; something very much like

  • 0

I’m trying to use pickle to save a custom class; something very much like the code below (though with a few methods defined on the class, and several more dicts and such for data). However, often when I run this, pickle and then unpickle, I lose whatever data was in the class, and its as if I created a new blank instance.

import pickle
class MyClass:
    VERSION = 1
    some_data = {}
    more_data = set()

    def save(self,filename):
        with open(filename, 'wb') as f:
            p = pickle.Pickler(f)
            p.dump(self)

    def load(filename):
        with open(filename,'rb') as ifile:
            u = pickle.Unpickler(ifile)
            obj = u.load()
            return obj

I was wondering if this had something to do with the memo of the pickle class, but I don’t feel like it should. When it doesn’t work, I look at my generated file and it looks something like this: (Obviously not meant to be readable, but it obviously contains no data)

€c__main__
MyClass
q

Anyways, I hope this is enough for someone to understand what might possibly be going on here, or what to look at.

  • 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-18T06:21:38+00:00Added an answer on June 18, 2026 at 6:21 am

    The problem you’re having is that you’re using mutable class variables to hold your data, rather than putting the data into instance variables.

    The pickle module only saves the data stored directly on the instance, not class variables that can also be accessed via self. When you’re finding your unpickled instance have no data, what that probably means is that the class doesn’t hold the data from the previous run, so the instances can’t access it any more.

    Using class variables that way will probably cause you other problems too, as the data will be shared by all instances of the class! Here’s a Python console session code that illustrates the issue:

    >>> class Foo(object):
            class_var = []
            def __init__(self, value):
                self.class_var.append(value)
    
    >>> f1 = Foo(1)
    >>> f1.class_var
    [1]
    >>> f2 = Foo(2)
    >>> f2.class_var
    [1, 2]
    

    That’s probably not what you wanted. But it gets worse!

    >>> f1.class_var
    [1, 2] 
    

    The data you thought had belonged to f1 has been changed by the creation of f2. In fact, f1.class_var is the very same object as f2.class_var (it is also available via Foo.class_var directly, without going through any instances at all).

    So, using a class variable is almost certainly not what you want. Instead, write an __init__ method for the class that creates a new value and saves it as an instance variable:

    >>> class Bar(object):
            def __init__(self, value):
                self.instance_var = [] # creates a separate list for each instance!
                self.instance_var.append(value)
    
    >>> b1 = Bar(1)
    >>> b1.instance_var
    [1]
    >>> b2 = Bar(2)
    >>> b2.instance_var # doesn't include value from b1
    [2]
    >>> b1.instance_var # b1's data is unchanged
    [1]
    

    Pickle will handle this class as you expect. All of its data is in the instances, so you should never end up with an empty instance when you unpickle.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.