I need to create a dummy object to mark an uninitialized element in a list (obviously, dictionary would have been a better choice, but I need a list because of heavy memory constraints).
I am thinking to use for this purpose an object with the following properties:
- It doesn’t evaluate as equal to any other object but itself.
- A reference to it cannot be created other than from the original reference (through assignment or parameter passing).
None satisfies the first but not the second requirement (as it may created anywhere in the program by using literal None).
One approach that should work, I think, is:
UNINITIALIZED_VALUE = object() # will be compared for == through identity
a = [1, UNINITIALIZED_VALUE, 2, UNINITIALIZED_VALUE]
a[1] == a[3] # True
a[0] == UNINITIALIZED_VALUE # False
I wanted to double check that I’m not missing any potential problem with this approach. (I’m using Python 3.)
No, that’s how everybody does it. Do note that you can’t set arbitrary attributes on such an object though.