Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think what you're asking is how to test object <em>identity</em> rather than object <em>equality</em>.</p> <p>In Python you can do this with the <a href="http://docs.python.org/reference/expressions.html#is" rel="nofollow"><code>is</code> operator</a>, i.e.</p> <pre><code>if x is y: </code></pre> <p>Rather than:</p> <pre><code>if x == y: </code></pre> <p>However, you're going have problems with ints and strings because the Python runtime automatically re-uses objects it creates for these from a pool:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; b = 1 &gt;&gt;&gt; a is b True &gt;&gt;&gt; id(a),id(b) (13561552, 13561552) </code></pre> <p>I think short strings are automatically "interned" in this way and you can force longer strings to be using the <a href="http://docs.python.org/library/functions.html#intern" rel="nofollow"><code>intern()</code> function</a>:</p> <pre><code>&gt;&gt;&gt; c = 'Long string' &gt;&gt;&gt; d = 'Long string' &gt;&gt;&gt; c is d False &gt;&gt;&gt; c = intern(c) &gt;&gt;&gt; d = intern(d) &gt;&gt;&gt; c is d True </code></pre> <p>However, it seems the <code>long</code> type isn't interned so you use that:</p> <pre><code>&gt;&gt;&gt; a = 1L &gt;&gt;&gt; b = 1L &gt;&gt;&gt; a is b False </code></pre> <p>However, object identity for built-in types is down to the implementation of Python rather than being in the language, and is not something you should rely on either way. I think you would be better off creating your own class of objects and writing an appropriate <code>__eq__</code> method for them, i.e. write your code it works based on object equality rather than identity.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload