viterbi
viterbi

Reputation: 429

Compute class and instance hash

I need to compute a "hash" which allows me to uniquely identify an object, both it's contents and the parent class.

By comparing these "hashes" i want to be able to tell if an object has changed since the last time it was scanned.

I have found plenty of examples on how to make an object hashable, however not so much about how to compute a hash of the parent class.

It is important to note comparisons are made during different executions. I say this because I think comparing the id() of the object since the id/address of the object might be different for different executions.

I thought of resorting to inspect but I fear it might not be very efficient, also I am not very sure how that would work if the object's parent class is inheriting from another class.

If I had access to the actual memory raw data where the instance and the class' code is stored, I could just compute the hash of that.

Any ideas?

Upvotes: 3

Views: 599

Answers (2)

vav
vav

Reputation: 4694

General idea is to serialize object and then take a hash. Then, the only question is to find a good library. Let's try dill:

>>>import dill
>>>class a():
    pass
>>>b = a()
>>>b.x = lambda x:1
>>> hash(dill.dumps(b))
2997524124252182619
>>> b.x = lambda x:2
>>> hash(dill.dumps(b))
5848593976013553511
>>> a.y = lambda x: len(x)
>>> hash(dill.dumps(b))
-906228230367168187
>>> b.z = lambda x:2
>>> hash(dill.dumps(b))
5114647630235753811
>>> 

Looks good?

dill: https://github.com/uqfoundation

Upvotes: 1

lucasnadalutti
lucasnadalutti

Reputation: 5948

To detect if an object has changed, you could generate a hash of its JSON representation and compare to the latest hash generated by the same method.

import json

instance.foo = 5
hash1 = hash(json.dumps(instance.__dict__, sort_keys=True))

instance.foo = 6
hash2 = hash(json.dumps(instance.__dict__, sort_keys=True))

hash1 == hash2
>> False

instance.foo = 5
hash3 = hash(json.dumps(instance.__dict__, sort_keys=True))

hash1 == hash3
>> True

Or, since json.dumps gives us a string, you can simply compare them instead of generating a hash.

import json

instance.foo = 5
str1 = json.dumps(instance.__dict__, sort_keys=True)

instance.foo = 6
str2 = json.dumps(instance.__dict__, sort_keys=True)

str1 == str2
>> False

Upvotes: 0

Related Questions