<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hauru &#187; craftsmanship</title>
	<atom:link href="http://hauru.eu/tag/craftsmanship/feed/" rel="self" type="application/rss+xml" />
	<link>http://hauru.eu</link>
	<description>Personal techblog by Tomek Paczkowski</description>
	<lastBuildDate>Wed, 07 Apr 2010 22:49:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Equality is not always sufficient</title>
		<link>http://hauru.eu/2009/11/22/equality-is-not-always-sufficient/</link>
		<comments>http://hauru.eu/2009/11/22/equality-is-not-always-sufficient/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 23:59:57 +0000</pubDate>
		<dc:creator>oinopion</dc:creator>
				<category><![CDATA[Techblog]]></category>
		<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[py-jutsu]]></category>

		<guid isPermaLink="false">http://hauru.eu/?p=103</guid>
		<description><![CDATA[I&#8217;ve just got hit by one of really typical errors while writing simple value type in Python. Consider following code: class Simple(object): def __init__(self, val): self.val = val def __eq__(self, other): return self.val == other.val The class Simple overrides equality operator delegating it to it&#8217;s value. Seems dead simple and easy for the first time. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just got hit by one of really typical errors while writing simple value type in Python. Consider following code:</p>
<pre><code>class Simple(object):
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val
</code></pre>
<p>The class <code>Simple</code> overrides equality operator delegating it to it&#8217;s value. Seems dead simple and easy for the first time. But not quite so. What will following snippet print?</p>
<pre><code>a, b = Simple(3), Simple(3)
set_a, set_b = set([a]), set([b])
print 'a == b: ', a == b
print 'set_a == set_b', set_a == set_b
</code></pre>
<p>You&#8217;ve guessed it: <code>True</code>, <code>False</code>. This is because two sets may be equal only if all elements <strong>hashes</strong> are equal. So I thought I will fix it with this:</p>
<pre><code>class Hashimple(object):
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val
    def __hash__(self):
        return (self.val * 5) + 7
</code></pre>
<p>Seems better, but look at this snippet and guess once more:</p>
<pre><code>a, b = Hashimple(3), Hashimple(3)
set_a, set_b = set([a]), set([b])
print 'Hashimple'
print 'a == b: ', a == b
print 'set_a == set_b', set_a == set_b
print 'a != b', a != b
</code></pre>
<p><code>True</code>, <code>True</code>, <code>True</code>?!? Well, Python will not imply that <code>a&nbsp;!=&nbsp;b</code> , not even when <code>a&nbsp;==&nbsp;b</code>.</p>
<p>This bug has been made thousands times by thousands of coders, it occurs not only in Python, but also in various other languages (Java, Ruby), it&#8217;s been described in lots of books, but I just keep forgetting about it. Maybe writing a blog post will help me remember?</p>
<p>Docs: <a href="http://docs.python.org/reference/datamodel.html#object.__eq__"><code>__eq__</code></a>, <a href="http://docs.python.org/reference/datamodel.html#object.__hash__"><code>__hash__</code></a></p>
]]></content:encoded>
			<wfw:commentRss>http://hauru.eu/2009/11/22/equality-is-not-always-sufficient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
