<br><br><div class="gmail_quote">On Wed, Oct 14, 2009 at 1:37 PM, Laszlo Nagy <span dir="ltr"><<a href="mailto:gandalf@shopzeus.com">gandalf@shopzeus.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div><div class="h5">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Andre Engels schrieb:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br></blockquote>
None, True, False, NotImplemented are guaranteed to be singletons, all<br>
builtin types and exceptions can be considered as singletons, too.<br>
  <br>
</blockquote></div></div>
I thought that different mutable objects always have different ids. If this is not true, then what the id() function is used for? What useful thing can we do with it?<br></blockquote><div><br></div><div>They do. </div><div>

<br></div><div>None, True, False, integers and strings are not mutable. The only time the id is the "same" between two objects is if they are the identical two objects.</div><div><br></div><div>CPython just (as a performance optimization) re-uses the same objects sometimes even if people think they're using different objects.</div>

<div><br></div><div>In,</div><div>>>> a = 2</div><div>>>> b = 2</div><div><br></div><div>The id is the same, as they're the same integer object. Its not two separate integer objects. Their ID will be the same, as they're precisely the same object.</div>

<div><br></div><div>But,</div><div>>>> a = 1e100</div><div>>>> b = 1e100</div><div><br></div><div>Those are two separate objects, because Python's not re-using an existing object the second time it sees 1e100.</div>

<div><br></div><div>Christian's point is, I believe, that this is all an implementation detail to the CPython platform and not a language-defined feature. Other implementations may do other things, so one should not rely on this behavior. Basically, don't use "is" unless you really know what you're doing -- or are testing verses a singleton :) "is" is never the right thing for numbers. Usually. Ahem.</div>

<div><br></div><div>--S</div></div>