Project

General

Profile

« Previous | Next » 

Revision 2670

dicts.py: Added IdCompared

View differences:

lib/dicts.py
18 18
    
19 19
    def __setitem__(self, key, value): self.inner[key] = value
20 20

  
21
class IdCompared:
22
    '''A value that's compared using `is` instead of ==.
23
    Do not use this for strings as they are interned, causing `is` to have the
24
    same meaning as ==.
25
    '''
26
    
27
    def __init__(self, value): self.value = value
28
    
29
    def __eq__(self, other):
30
        return (other != None and other.__class__ == self.__class__
31
            and other.value is self.value)
32
    
33
    def __hash__(self): return id(self.value)
34

  
21 35
class IdDict(dict):
22 36
    '''A dict that stores objects by id()'''
23 37
    

Also available in: Unified diff