0

I have a class where one of the methods is to replace the entire data encapsulate in the object when needed. However, I am not sure how to write the update the data to the object. Is it possible to assign the instance to the new instance, maybe something like self=new_self? I am aware I can create a new instance and then change the ptr to point to it, but would like to check if I can just update the same instance instead.

class Dictionary:
 def __init__(self, collection_size):
 self.dictionary = [Term() for _ in range(collection_size)]
 self.collection_size = collection_size
 def load(self, src):
 with open(src, 'rb') as handle:
 self = pickle.load(handle) # this is the part that needs editing
S.B
17.1k12 gold badges38 silver badges74 bronze badges
asked Mar 9, 2022 at 21:14
3
  • 2
    "possible to assign the instance to the new instance, maybe something like self=new_self" No, that doesn't make sense. You assign to names. Names refer to objects. You dont' assign objects to objects. Commented Mar 9, 2022 at 21:17
  • Does this help? stackoverflow.com/a/1216361/2681662 Commented Mar 9, 2022 at 21:17
  • One way is to write a method that updates your Dictionary object given another Dictionary object, something like self.dictionary = other.dictionary and self.collection_size = other.collection_size Commented Mar 9, 2022 at 21:18

2 Answers 2

1

I think a class method probably makes the most sense, something like

class Dictionary:
 def __init__(self, collection_size):
 self.dictionary = [Term() for _ in range(collection_size)]
 self.collection_size = collection_size
 @classmethod
 def load_from_pickle(cls, src):
 with open(src, 'rb') as handle:
 cls = pickle.load(handle)
 return cls
loaded_dictionary = Dictionary.load_from_pickle('path/to/pickle')

You probably want some "guardrails" on the load method to make sure you're unpickling an object that works.

At some point, you may also want some kind of Dictionary.merge, but that seems independent of initializing from a pickled object.

answered Mar 9, 2022 at 21:45
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for all the responses. I am using a mix of Andrew and Juanpa's answers. Because of the way my data is processed, it is unnecessary to do a merge but that + adding try/except to load_from_pickle is a very good point. This is what I have settled on. Caveat: unsure if it is taboo to call init again.

class Dictionary:
def __init__(self, collection_size=None, dictionary=None):
 self.dictionary = dictionary if dictionary else {}
 self.collection_size = collection_size
 self.collection_postings_list_ptr = 0
def load(self, src):
 with open(src, 'rb') as handle:
 new_dictionary = pickle.load(handle)
 self.__init__(new_dictionary.collection_size, new_dictionary.dictionary)
answered Mar 10, 2022 at 1:08

1 Comment

That works, though without getting too much into code review, the situations where you may want a load independent of class instantiation, like you are here, is if you're only loading some pieces of the class (e.g. data) while other attributes (e.g. flow controls) are made on initialization. Here, you're essentially recreating the class method, but with more obscurity. But nothing is inherently "wrong" with it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.