Why is the id of a Python class not unique when called quickly?

前端 未结 6 1175
花落未央
花落未央 2020-11-22 15:29

I\'m doing some things in Python (3.3.3), and I came across something that is confusing me since to my understanding classes get a new id each time they are called.

6条回答
  •  生来不讨喜
    2020-11-22 15:59

    I sense a deeper problem here. You should not be relying on id to track unique instances over the lifetime of your program. You should simply see it as a non-guaranteed memory location indicator for the duration of each object instance. If you immediately create and release instances then you may very well create consecutive instances in the same memory location.

    Perhaps what you need to do is track a class static counter that assigns each new instance with a unique id, and increments the class static counter for the next instance.

提交回复
热议问题