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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:16:40+00:00 2026-06-07T03:16:40+00:00

import cPickle class player: level = 1 exp = 0 inven = [] char

  • 0
import cPickle

class player:
    level = 1
    exp = 0
    inven = []

char = player()

#Append items to char.inven later in program

def save():
    file = open('savegame.dat', 'w')
    cPickle.dump(char, file)

def load():
    s_g = open('savegame.dat', 'r')
    char = cPickle.load(s_g)
    s_g.close()

This is a very small portion of the actual code, I cut it down to show where it’s going wrong.

When I save/ load it, it doesn’t keep the items in char.inven, even when I specifically dump the list into the file. Any idea why it erases the list’s items?

Also, if I join the list before dumping it, it saves it. But some of the list items are 2 words, so list.split() makes it different from before

  • 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-07T03:16:41+00:00Added an answer on June 7, 2026 at 3:16 am

    inven is attribute of the class, not the instance. Pickle doesn’t save anything about the class; it just stores the name of the class to reinstantiate it later. I would guess that you don’t want inven to be a class attribute anyway, so you should be doing:

    class player:
        def __init__(self):
            self.level = 1
            self.exp = 0
            self.inven = []
    

    Edit: if you are seeing that level is saved, it’s probably because you’re rebinding it. If, for instance, you have code that does this:

    self.level += 1
    

    You are not incrementing the class attribute. You are creating a new instance attribute. Whenever you have an instance obj and you do obj,attr = ... you create an instance attribute, not a class attribute.

    The reason you’re not seeing the same effect with inven is because you’re probably mutating it (e.g., with self.inven.append(...)) rather than rebinding it. Thus you never create an instance attribute, you just keep modifying the class attribute.

    You should search on StackOverflow for questions about class versus instance attributes (also sometimes called class and instance variables) to understand how this works in Python.

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

Sidebar

Related Questions

In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
import time import threading class test(threading.Thread): def __init__ (self): threading.Thread.__init__(self) self.doSkip = False self.count
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class File { private JFrame frame1; private
import org.joda.time.LocalDate; public class Test { public static void main(String[] args) { long time=System.currentTimeMillis();
import java.awt.*; import javax.swing.*; import javax.swing.GroupLayout; /** * @author Ene Ion */ public class
import MySQLdb import sys from libdesklets.controls import Control from IDBConnection import IDBConnection class DBConnection(Control,
import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ExplicitChannelRead { /** * @param args
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import math import os class collection: col = [[0 for col in range(5)] for
import java.math.BigInteger; import java.util.HashMap; /** * * @author cypronmaya */ public class test {

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.