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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:33:57+00:00 2026-05-24T05:33:57+00:00

Alright, this is pretty fun. Code will illustrate best what I’m thinking about: class

  • 0

Alright, this is pretty fun.

Code will illustrate best what I’m thinking about:

class Restorable(object):
    def __init__(self, a=1, b=1):
        self.a = a
        self.b = b

    def restore(self, serialized_object):
        # I'm aware this is local, just illustrating
        self = pickle.loads(serialized_object)

r = Restorable()
r.a = 1000
r.b = 2000
saved_state = pickle.dumps(r)
r.a = -1
r.b = -1
r.restore(pickle.dumps(saved_state)
# r.a should again be 1000
# r.b should again be 2000

The idea is: would be possible to, say, pickle.dumps() an object, store it somewhere, modify the real object and later on restore it to its previous state through what I’ve safely stored?

I’ve tried the code I’ve shown above, it does temporarily set self to the previous state, but only locally – well, no surprise here.

This is a curiosity question.

EDIT: To formulate the question in a clear way:

Is it possible to write an object that would restore its state using a serialized object of the same type?

Actually, I see this can be done by creating an instance of the serialized object inside restore() and copying values one by one. But this could be tedious for large objects.

  • 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-24T05:33:58+00:00Added an answer on May 24, 2026 at 5:33 am

    As you said in your edit, create a new object (don’t assign to self, obviously), but instead of copying the attributes one by one you can

    def restore(self, serialized_object):
        original = pickle.loads(serialized_object)
        self.__dict__.update(original.__dict__)
    

    and update all your attributes at once. Here’s your complete (reworked) example:

    import pickle
    class Restorable(object):
        def __init__(self, a=1, b=1):
            self.a = a
            self.b = b
        def restore(self, serialized_object):
            original = pickle.loads(serialized_object)
            self.__dict__.update(original.__dict__)
    
    r = Restorable()
    r.a = 1000
    r.b = 2000
    saved_state = pickle.dumps(r)
    r.a = -1
    r.b = -1
    r.restore(saved_state)
    print r.a, r.b 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright so I have this C++ image capturing class. I was wondering if I
Alright, this is probably gonna be a pretty simple question to answer. I haven't
Alright so I'm pretty new to this rails stuff so please bear with me...
Alright, so warsow has some pretty excellent hud code with the exception that the
Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright so pretty basic, here are the pieces of code I have, basically it's
Is it alright to do this? $author = strtolower($_SESSION['valid_username']); I want to enter all
Alright, this one (3a; sample problem with provided answer) has got me scratching my
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, this specific layout is just annoying me. And can't seem to find a

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.