I’m wondering if there is anything about python object IDs that will prevent them from ever equaling zero? I’m asking because I’m using zero as a stand-in for a special case in my code.
-
3Can you elaborate on *using zero as a stand-in for a special case in my code*. What do you want to achieve? – Mike Müller Feb 08 '16 at 14:47
-
1According to the definition, it could be. You should not write any code that assumes this, in spite of CPython's or other's implementation that might make this ok. – Russia Must Remove Putin Feb 08 '16 at 14:51
-
help(id) is your friend. id returns address of memory allotted. Your virtual memory address would not be 0 or atleast isn't returned that way. Even if you said id(None) it would return a virtual address of a memory location in your Null page. I don't think you must be worried about it returning absolute 0. As Mike above said can you elaborate your intent ? – gusaki Feb 08 '16 at 14:52
-
Nope, there is no language guarantee. – msw Feb 08 '16 at 14:57
4 Answers
From the docs
CPython implementation detail: This is the address of the object in memory.
0
is an invalid memory location. So no object in C will ever have this memory location and no object in the CPython implementation will ever have an id
of zero.
Not sure about other python implementations though

- 4,958
- 1
- 26
- 35
-
1I don't think an implementation detail is strong enough, Python should be able to run correctly no matter the implementation. – Russia Must Remove Putin Feb 08 '16 at 14:50
-
1And the OP ought not fabricate a zero id, especially when there is a perfectly servicable `None` sentinel value. – msw Feb 08 '16 at 14:55
-
the reason I’m not using None is because I’m running hash() on the objects to get these numbers, and I don’t want to go through the trouble of determining if hash(None) came from a NoneType object when I could just set it to 0 and be sure of it – kelvinsong Feb 08 '16 at 15:54
Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
There's nothing that says that it cannot be zero (zero is an integer). If you rely on it not being zero then you're relying on a current implementation detail which is not smart.
What you instead should do is to use for example None
to indicate that it isn't an id
of an object.

- 13,817
- 1
- 35
- 57
-
```i = None, j = None``` giving ```id(i) == id(j)```, unsafe given the common use of None. ```special = object()``` does not suffer this weakness. – jwal Aug 26 '23 at 19:22
This isn't as strong an answer as I'd like, but doing help(id) on python 2.7.5
id(...) id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.)
Assuming you don't have an object that is pointing to NULL, you should be safe there.

- 6,148
- 11
- 40
- 42
-
An implementation detail – as opposed to a language specification – is *never* safe. – msw Feb 08 '16 at 14:58
-
@msw It's entirely safe if you never change which implementation you're using – Jon Chesterfield Feb 08 '16 at 15:17
If you want an object that is different than any other, you can create one:
special = object()
As long as you don't delete it, special
will be unique over the run time of your program. This might achieve the same thing you intend with checking id()
being zero.

- 82,630
- 20
- 166
- 161