len(var) is [CONSTANT] equal to len(var) == [CONSTANT]?

Duncan Booth duncan.booth at invalid.invalid
Thu Nov 23 12:52:20 CET 2006


Steven D'Aprano wrote:

> No, you will never get two objects existing at the same time with the
> same id. You will get two objects that exist at different times with
> the same id, since ids may be reused when the object is deleted.
> 
I think it is worth pointing out that this is an area where people get 
confused quite often; it is very easily to get misleading results when you 
call the id() function. e.g.

>>> class C:
	def f(self): pass
	def g(self): pass

	
>>> c = C()
>>> id(c.f)==id(c.g)
True
>>> c.f is c.g
False

The ids are the same here only because the objects do not exist at the same 
time. In the first comparison c.f is an expression which creates a 
temporary object that is destroyed before the expression involving c.g is 
evaluated, so it is possible for the different objects to have the same id. 
In the second comparison the objects exist at the same time so they are 
forced to have different ids.



More information about the Python-list mailing list