简体   繁体   English

根据python对象的属性生成python对象的唯一ID

[英]Generate unique ID for python object based on its attributes

Is there a way to generate a hash-like ID in for objects in python that is solely based on the objects' attribute values? 有没有办法在python中为对象生成一个类似哈希的ID,它只基于对象的属性值? For example, 例如,

class test:
    def __init__(self, name):
        self.name = name

obj1 = test('a')
obj2 = test('a')

hash1 = magicHash(obj1)
hash2 = magicHash(obj2)

What I'm looking for is something where hash1 == hash2. 我正在寻找的是hash1 == hash2。 Does something like this exist in python? python中是否存在类似的内容? I know I can test if obj1.name == obj2.name, but I'm looking for something general I can use on any object. 我知道我可以测试obj1.name == obj2.name,但我正在寻找一些我可以在任何对象上使用的通用。

You mean something like this? 你的意思是这样的? Using the special method __hash__ 使用特殊方法__hash__

class test:
     def __init__(self, name):
         self.name = name
     def __hash__(self):
         return hash(self.name)

>>> hash(test(10)) == hash(test(20))
False
>>> hash(test(10)) == hash(test(10))
True

To get a unique comparison: 要获得独特的比较:

To be unique you could serialize the data and then compare the serialized value to ensure it matches exactly. 要使其唯一,您可以序列化数据,然后比较序列化值以确保它完全匹配。

Example: 例:

import pickle

class C:
  i = 1
  j = 2

c1 = C()
c2 = C()
c3 = C()
c1.i = 99

unique_hash1 = pickle.dumps(c1) 
unique_hash2 = pickle.dumps(c2) 
unique_hash3 = pickle.dumps(c3) 

unique_hash1 == unique_hash2 #False
unique_hash2 == unique_hash3 #True

If you don't need unique values for each object, but mostly unique: 如果您不需要每个对象的唯一值,但大多数都是唯一的:

Note the same value will always reduce to the same hash, but 2 different values could reduce to the same hash. 请注意,相同的值将始终减少到相同的哈希值,但是2个不同的值可能会减少到相同的哈希值。

You cannot use something like the built-in hash() function (unless you override __hash__ ) 你不能使用内置的hash()函数(除非你重写__hash__

hash(c1) == hash(c2) #False
hash(c2) == hash(c3) #False <--- Wrong

or something like serialize the data using pickle and then use zlib.crc32. 或类似使用pickle序列化数据然后使用zlib.crc32。

import zlib
crc1 = zlib.crc32(pickle.dumps(c1))
crc2 = zlib.crc32(pickle.dumps(c2))
crc3 = zlib.crc32(pickle.dumps(c3))
crc1 == crc2 #False
crc2 == crc3 #True

Have a lool at the hash() build in function and the __hash__() object method . 在函数hash()构建__hash__()对象方法中有一个lool。 These may be just what you are looking for. 这些可能正是您所寻找的。 You will have to implement __hash__() for you own classes. 您必须为自己的类实现__hash__()

I guess 我猜

def hash_attr(ins):
 return hash(tuple(ins.__dict__.items()))

hashes anything instance based on its attributes. 基于其属性散列任何实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM