0

Using

a=7
print hex(id(a))

getting back:

0x7f866b425e58

Intending to use this output (converted to string) as an identification number for each class object declared I need to make sure it would stay the same while the program is running. I also need to know if a second character is always 'x' (on mac, win and linux). If I am making a bad choice choosing hex(id)) please let me know what would be a better option.

2 Answers 2

7

id() is guaranteed to be stable for the lifetime of the object, but the identifier can be reused if the object is destroyed.

The 0x prefix is always going to be there when using hex(). You could also use format(..., 'x') to format to a hex number without a prefix:

>>> a = 7 
>>> print format(id(a), 'x')
7f866b425e58

You'd be better of just using a itertools.count() object to produce your unique ids, perhaps with a prefix or by negating the number if you need to distinguish these from others:

from itertools import count

id_counter = count()

and each time you need a new id, use:

next(id_counter)

and store this as an attribute on your instances. You can make the count() count down by giving it a negative step:

id_counter = count(-1, -1)

Demo:

>>> from itertools import count
>>> id_counter = count(-1, -1)
>>> next(id_counter)
-1
>>> next(id_counter)
-2
Sign up to request clarification or add additional context in comments.

1 Comment

I like an idea of using itertools.count()! Thanks! That's a great one! Actually I was already using negative numbers to distinguish the objects that were not yet written to database. It is exactly what I need.
-1

You might want to look at hash(obj) https://docs.python.org/2/library/functions.html#hash

Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).

9 Comments

Hash identifies a value, and not uniquely. The OP wants to identify specific objects.
OP said "...I need to make sure it would stay the same while the program is running...". I wasn't 100% sure if he wanted identity or value.
@MartijnPieters But is that really what the OP needs? This smells to me like an XY question.
@jamessan: I agree it'd be helpful for the OP to clarify their usecase.
All the object instances are database entities. Each has an assigned by database unique id number. Another group of such instances are the ones that were declared but not yet written to database (no database id number yet). An entire code designed around the unique id numbers. The instances with no ids (unpublished ones) fall out of the entire design. So I need to be able to substitute yet-not-existent database ids with the fake ones.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.