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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:50:56+00:00 2026-06-05T00:50:56+00:00

Suppose I have a simple python class definition in a file myClass.py class Test:

  • 0

Suppose I have a simple python class definition in a file myClass.py

class Test:
    A = []

And I also have two test scripts. The first script creates an object of type Test, populates the array A, and pickles the result to a file. It immediately unpickles it from the file and the array is still populated.
The second script just unpickles from the file, and the array is not populated (i.e. A == []). Why is this?

test1.py

import myClass
import pickle

x = myClass.Test()

for i in xrange(5):
    x.A.append(i)

f = open('data', 'w')
pickle.dump(x,f)
f.close()

f = open('data')
y = pickle.load(f)
f.close

print y.A

and test2.py

import myClass
import pickle

f = open('data')
y = pickle.load(f)
f.close

print y.A
  • 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-05T00:50:58+00:00Added an answer on June 5, 2026 at 12:50 am

    It is because you are setting Test.A as a class attribute instead of an instance attribute. Really what is happening is that with the test1.py, the object being read back from the pickle file is the same as test2.py, but its using the class in memory where you had originally assigned x.A.

    When your data is being unpickled from the file, it creates a new instance of the class type, and then applies whatever instance data it needs to. But your only data was a class attribute. Its always referring back to the class thats in memory, which you modified in one, but not in another file.

    Compare the differences in this example:

    class Test:
        A = []  # a class attribute
        def __init__(self):
            self.a = []  # an instance attribute
    

    You will notice that the instance attribute a will be pickled and unpickled properly, while the class attribute A will simply refer to the class in memory.

    for i in range(5):
        x.A.append(i)
        x.a.append(i)  
    
    with open('data', 'wb') as f:
        pickle.dump(x,f)
    
    with open('data', 'rb') as f:
        y = pickle.load(f)
    
    >>> y.A
    [0, 1, 2, 3, 4]
    >>> y.a
    [0, 1, 2, 3, 4]
    >>> Test.A
    [0, 1, 2, 3, 4]
    >>> Test.A = []  # resetting the class attribute
    >>> y.a 
    [0, 1, 2, 3, 4]
    >>> y.A  # refers to the class attribute
    []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose I have a perl (or python) script that does something highly secretive; I'd
Suppose I have a simple Python list like this: >>> l=['0', '1', '2', '3',
Suppose I have this simple app working : # model class Project has_many :numbers
Suppose I have this simple class: class Color attr_accessor :rgb def initialize(ary) @rgb =
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
I have one simple app that suppose to get information from user and send
Lets suppose that I have the following simple query var q = from p
i have this class called MemoryManager, it is supposed to implement a simple smart

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.