<?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; py-jutsu</title>
	<atom:link href="http://hauru.eu/tag/py-jutsu/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>Python&#8217;s Augmented Assignments</title>
		<link>http://hauru.eu/2010/02/15/python-augmented-assignments/</link>
		<comments>http://hauru.eu/2010/02/15/python-augmented-assignments/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 19:12:20 +0000</pubDate>
		<dc:creator>oinopion</dc:creator>
				<category><![CDATA[Techblog]]></category>
		<category><![CDATA[py-jutsu]]></category>

		<guid isPermaLink="false">http://hauru.eu/?p=123</guid>
		<description><![CDATA[Everyone knows that in Python variables are just references and passing such reference to function effectively copies value of the reference to functions local scope, hence any assignment within function has no effect on original variable. Knowing that, this took me by surprise: >>> def add_eggs(foo): ... foo += ['eggs'] ... >>> spam = ['spam'] [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone knows that in Python variables are just references and passing such reference to function effectively copies value of the reference to functions local scope, hence any assignment within function has no effect on original variable. Knowing that, this took me by surprise:</p>
<pre><code>
>>> def add_eggs(foo):
...     foo += ['eggs']
...
>>> spam = ['spam']
>>> add_eggs(spam)
>>> print spam
['spam', 'eggs']
</code></pre>
<p>This seemed to me as an error in Python interpreter, but after checking language reference I discovered it&#8217;s not; it&#8217;s intended behaviour (emphasis is mine): </p>
<blockquote><p>
An augmented assignment expression like <tt>x += 1</tt> can be rewritten as <tt>x = x + 1</tt> to achieve a similar, but <strong>not exactly equal effect</strong>. In the augmented version, <tt>x</tt> is only evaluated once.  Also, when possible, <strong>the actual operation is performed in-place</strong>, meaning that rather than creating a new object and assigning that to the target, <strong>the old object is modified</strong> instead.
</p></blockquote>
<p>Quoted after: <a href="http://docs.python.org/reference/simple_stmts.html#augmented-assignment-statements">python docs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hauru.eu/2010/02/15/python-augmented-assignments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Failed with Google App Engine</title>
		<link>http://hauru.eu/2008/07/14/failed-with-google-app-engine/</link>
		<comments>http://hauru.eu/2008/07/14/failed-with-google-app-engine/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 19:13:27 +0000</pubDate>
		<dc:creator>oinopion</dc:creator>
				<category><![CDATA[Techblog]]></category>
		<category><![CDATA[py-jutsu]]></category>

		<guid isPermaLink="false">http://blog.hauru.eu/?p=3</guid>
		<description><![CDATA[I&#8217;m writing a little app for my English course at Jagiellonian University in Django. Right now I&#8217;m hosting it here, on hauru.eu, but soon we&#8217;ll release a book we were producing whole year and thus I have to finish the app and make it public. I thought about putting it on Google App Engine  (think: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing a little app for my English course at <a href="http://www.uj.edu.pl/">Jagiellonian University</a> in <a href="http://www.djangoproject.com/">Django</a>. Right now I&#8217;m hosting it here, on hauru.eu, but soon we&#8217;ll release a book we were producing whole year and thus I have to finish the app and make it public. I thought about putting it on <a href="http://code.google.com/appengine/">Google App Engine</a>  (think: free hosting in Python), but after two days of trying I must say I don&#8217;t see any point any more.</p>
<p>I wrote my app using many of convenience methods/classes provided by Django, but very few of them are supported by GAE. I&#8217;d have to rewrite half of code! No way. It&#8217;s right time to use some servers in <a href="http://www.ksi.ii.uj.edu.pl/">KSI</a>: Students&#8217; Computer Science Club, which I&#8217;m proud member of.</p>
<p>I&#8217;m sure Google App Engine is powerful and convenient platform, but I don&#8217;t think Django fits there well. While reading about GAE I thought the best solution would be using some external libraries like <a href=" http://werkzeug.pocoo.org/">Werkzeug</a>, as GAE is based on WSGI interface.</p>
<p><strong>Update (Dec 13, 2008)</strong>: The little app is live and indeed running on KSIs server. Check it out, it&#8217;s called <a href="http://englishplusplus.jcj.uj.edu.pl">English++: English for Computer Science Students</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hauru.eu/2008/07/14/failed-with-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
