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
  • 13,077
  • 10
  • 22
  • 49
xiaogou
  • 3
  • 1
  • 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. – juanpa.arrivillaga Mar 09 '22 at 21:17
  • Does this help? https://stackoverflow.com/a/1216361/2681662 – MSH Mar 09 '22 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` – juanpa.arrivillaga Mar 09 '22 at 21:18

2 Answers2

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.

Andrew Holmgren
  • 1,225
  • 1
  • 11
  • 18
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)
xiaogou
  • 3
  • 1
  • 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. – Andrew Holmgren Mar 10 '22 at 16:36