How to reuse an instance of hashlib.md5 How to reuse an instance of hashlib.md5 python python

How to reuse an instance of hashlib.md5


Why do you think it's inefficient to make a new one? It's a small object, and objects are created and destroyed all the time. Use a new one, and don't worry about it.


Here's what I did, just write a little wrapper that reinitializes the hash object. Handles the clunkiness of the code writing, but maybe not the efficiency at runtime.

def Hasher(object):    def __init__(self):        self.md5 = hashlib.md5()    def get_hash(self, o):        self.md5.update(o)        my_hash = self.md5.digest()        self.md5 = hashlib.md5()        return my_hash