<?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>Algorithm.co.il &#187; gotchas</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/programming/gotchas/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Python Gotchas 1: __del__ is not the opposite of __init__</title>
		<link>http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 07:18:17 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[gotchas]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Gotcha]]></category>
		<category><![CDATA[__del__]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=438</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/' addthis:title='Python Gotchas 1: __del__ is not the opposite of __init__'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>After discussing my last post with a friend and talking about a few other issues, we came to the conclusion that it would be worthwhile to discuss more gotchas. First though, what is a gotcha? Wikipedia gives a good definition: &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/' addthis:title='Python Gotchas 1: __del__ is not the opposite of __init__' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>After discussing my <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/generatorexit-another-reason-to-upgrade-to-python-2-6/">last post</a> with a friend and talking about a few other issues, we came to the conclusion that it would be worthwhile to discuss more gotchas.</p>
<p>First though, what is a gotcha? Wikipedia gives a <a href="http://en.wikipedia.org/wiki/Gotcha_%28programming%29">good definition</a>:</p>
<blockquote><p>In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome.</p></blockquote>
<p>So let&#8217;s start with &#8220;__del__ is not the opposite of __init__&#8221;.<br />
If you come from c++ or a similar background, you are probably well versed in object oriented concepts, specifically, constructors and destructors. The usual expectation is to have the destructor called only for fully constructed objects &#8211; i.e., objects whose constructor returned without raising an exception.</p>
<p>If the constructor raises an exception, it is expected to &#8220;clean up after itself&#8221;, and not expect the destructor to run.</p>
<p>Since in Python __init__ is the de-facto constructor, and __del__ is considered the destructor, most people expect this line of reasoning to work with __init__ and __del__.<br />
This is mistaken. __del__ is not the opposite of __init__, but rather of __new__. Which means that if __init__ raises an exception, then __del__ will still be called.<br />
I&#8217;ve run into this issue myself several times in the past. Consider the following sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> A<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,x<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> x == <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">x</span> = x
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__del__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">x</span></pre></div></div>

<p>This code demonstrates a common case: a constructor that might fail, and a destructor that does something with the instance&#8217;s members. If you try to instantiate A with x = 0, you&#8217;ll get an exception. This is to be expected.<br />
However, what is less expected is when the partially constructed A is garbage-collected (which may be anytime later, and not necessarily right away):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">Exception</span> <span style="color: #dc143c;">exceptions</span>.<span style="color: #008000;">AttributeError</span>: <span style="color: #483d8b;">&quot;'A' object has no attribute 'x'&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>bound
 method A.<span style="color: #0000cd;">__del__</span> of <span style="color: #66cc66;">&lt;</span>__main__.<span style="color: black;">A</span> <span style="color: #008000;">object</span> at 0x02449570<span style="color: #66cc66;">&gt;&gt;</span> ignored</pre></div></div>

<p>What happened is that __del__ was called even though __init__ raised an exception. When __del__ tried to access self.x it got an attribute error, because it hasn&#8217;t been defined yet.</p>
<p>The solution?<br />
1. Don&#8217;t use __del__ unless you really have to. I&#8217;m going to write a more about it soon.<br />
2. If you do use __del__ make sure you are covered for any case in which __init__ didn&#8217;t finish running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

