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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:20:30+00:00 2026-05-16T02:20:30+00:00

Currently, I have a dictionary that has a number as the key and a

  • 0

Currently, I have a dictionary that has a number as the key and a Class as a value. I can access the attributes of that Class like so:

dictionary[str(instantiated_class_id_number)].attribute1

Due to memory issues, I want to use the shelve module. I am wondering if doing so is plausible. Does a shelve dictionary act the exact same as a standard dictionary? If not, how does it differ?

  • 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-16T02:20:30+00:00Added an answer on May 16, 2026 at 2:20 am

    Shelve doesn’t act extactly the same as dictionary, notably when modifying objects that are already in the dictionary.

    The difference is that when you add a class to a dictionary a reference is stored, but shelve keeps a pickled (serialized) copy of the object. If you then modify the object you will
    modify the in-memory copy, but not the pickled version. That can be handled (mostly) transparently by shelf.sync() and shelf.close(),
    which write out entries. Making all that work does require tracking all retrieved objects which haven’t been written back yet so you do have to call shelf.sync() to clear the cache.

    The problem with shelf.sync() clearing the cache is that you can keep a reference to the object and modify it again.

    This code doesn’t work as expected with a shelf, but will work with a dictionary:

    s["foo"] = MyClass()
    s["foo"].X = 8 
    p = s["foo"] # store a reference to the object
    p.X = 9 # update the reference
    s.sync() # flushes the cache
    p.X = 0
    print "value in memory: %d" % p.X # prints 0
    print "value in shelf: %d" % s["foo"].X # prints 9
    

    Sync flushes the cache so the modified ‘p’ object is lost from the cache so it isn’t written back.

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

Sidebar

Related Questions

I currently have an MS Access application that connects to a PostgreSQL database via
I have code that has a Dictionary defined as: Dictionary<int, StringBuilder> invoiceDict = new
I have a class whose instances have attributes that are containers which themselves contain
I have a huge dictionary of blank values in a variable called current like
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a class and I'm trying to create an easy GUI to
In one of my classes I have a number of methods that all draw
I currently have some python code I'd like to port to C++ as it's
I have a function that has several outputs, all of which native, i.e. integers
There has to be a cleaner method. Currently I have: ... Constructor() { parseDictionary

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.