<?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>UncleZeiv&#039;s Corner &#187; tips</title>
	<atom:link href="http://unclezeiv.kerid.org/tag/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://unclezeiv.kerid.org</link>
	<description>still in love with underscores</description>
	<lastBuildDate>Sun, 15 Jan 2012 22:30:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>About getters and setters in Python</title>
		<link>http://unclezeiv.kerid.org/2009/08/about-getters-and-setters-in-python?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=about-getters-and-setters-in-python</link>
		<comments>http://unclezeiv.kerid.org/2009/08/about-getters-and-setters-in-python#comments</comments>
		<pubDate>Tue, 25 Aug 2009 18:43:34 +0000</pubDate>
		<dc:creator>UncleZeiv</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[getters]]></category>
		<category><![CDATA[property]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pythonic]]></category>
		<category><![CDATA[setters]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://unclezeiv.kerid.org/?p=91</guid>
		<description><![CDATA[Recently I&#8217;ve been developing a fairly large Python application, involving a somewhat articulate and deep class hierarchy. With time, I ended up writing a certain amount of getters and setters which felt suspiciously unpythonic. Looking for better solutions I stumbled upon a number of sources making fun of people writing getters and setters in Python [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been developing a fairly large Python application, involving a somewhat articulate and deep class hierarchy. With time, I ended up writing a certain amount of getters and setters which felt suspiciously <em>unpythonic</em>. Looking for better solutions I stumbled upon a number of sources making fun of people writing getters and setters in Python (e.g. <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" target="_blank">Python Is Not Java</a>), because:</p>
<ul>
<li>there&#8217;s <strong>no real data member protection</strong> in Python anyway (&#8220;we are all consenting adults here&#8221;)</li>
<li>if you later want to embed some logic in the retrieval of an attribute, <strong>you can keep the same interface</strong> by just adding a <a href="http://docs.python.org/library/functions.html#property" target="_blank">property</a></li>
</ul>
<p>These arguments were so compelling that made me feel pretty stupid. Consequently, I immediately started converting all my getters and setters into variables or, when needed, into properties.</p>
<p>Unfortunately, it soon became apparent that I had not considered all the facets of the problem. Problems surfaced soon, primarily because, as I had to find out the hard way, <strong>avoiding getters/setters does not play <em>that</em> well with inheritance</strong>.</p>
<p>First of all, overriding the access of an attribute becomes a bit quirky &#8212; despite being perfectly acceptable when using getters. In particular, the child class must know if a certain attribute is indeed an attribute or a property &#8212; and this already breaks the promise of keeping the switch from variable to property a local change.</p>
<div class="codecolorer-container python default codecolorer-noborder" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span><span style="color: #008000;">property</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">5</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">class</span> Bar<span style="color: black;">&#40;</span>Foo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span><span style="color: #008000;">property</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> Foo.<span style="color: black;">foo</span>.<span style="color: black;">fget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> Foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">foo</span> <span style="color: #808080; font-style: italic;"># 5</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> Bar<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">foo</span> <span style="color: #808080; font-style: italic;"># 6</span></div></td></tr></tbody></table></div>
<p>The syntax gets really cumbersome. For instance, if you want to override only the getter <em>or</em> the setter, verbosity ensues:</p>
<div class="codecolorer-container python default codecolorer-noborder" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span><span style="color: #008000;">property</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">5</span><br />
<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span>foo.<span style="color: black;">setter</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> val<br />
<br />
<span style="color: #808080; font-style: italic;"># one way to do that</span><br />
<span style="color: #ff7700;font-weight:bold;">class</span> Bar1<span style="color: black;">&#40;</span>Foo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span><span style="color: #008000;">property</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> Foo.<span style="color: black;">foo</span>.<span style="color: black;">fget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><br />
<br />
&nbsp; &nbsp; <span style="color: #66cc66;">@</span>foo.<span style="color: black;">setter</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> Foo.<span style="color: black;">foo</span>.<span style="color: black;">fset</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;"># another way</span><br />
<span style="color: #ff7700;font-weight:bold;">class</span> Bar2<span style="color: black;">&#40;</span>Foo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo_get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> Foo.<span style="color: black;">foo</span>.<span style="color: black;">fget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> foo_set<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> Foo.<span style="color: black;">foo</span>.<span style="color: black;">fset</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; foo <span style="color: #66cc66;">=</span> <span style="color: #008000;">property</span><span style="color: black;">&#40;</span>foo_get<span style="color: #66cc66;">,</span> foo_set<span style="color: black;">&#41;</span><br />
<br />
Foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">4</span><br />
Bar1<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">3</span><br />
Bar2<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">2</span></div></td></tr></tbody></table></div>
<p>Granted, true Pythoners would say that this is not Python&#8217;s fault but mine, as I have evidently made some wrong, or at least unpythonic, design decisions at a higher level. That&#8217;s an acceptable criticism, but I still think that the organization of my program was fair when employing getters and setters. Most importantly, though, the consequences of substituting them in class hierarchies where overriding is common have not been sufficiently explained by the previously mentioned Internet sources.</p>
<p>In other words, sometimes &#8220;<em><a href="http://www.python.org/dev/peps/pep-0020/">practicality beats purity</a></em>&#8220;!</p>
]]></content:encoded>
			<wfw:commentRss>http://unclezeiv.kerid.org/2009/08/about-getters-and-setters-in-python/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Use ack!</title>
		<link>http://unclezeiv.kerid.org/2008/05/use-ack?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=use-ack</link>
		<comments>http://unclezeiv.kerid.org/2008/05/use-ack#comments</comments>
		<pubDate>Tue, 06 May 2008 23:39:44 +0000</pubDate>
		<dc:creator>UncleZeiv</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://unclezeiv.kerid.org/?p=15</guid>
		<description><![CDATA[By misspelling awk during a search, I&#8217;ve come to know a very useful utility: ack. I wholeheartedly suggest to install it and use it as a drop in replacement for grep in most circumstances. Basically, it&#8217;s a developer-friendly grep that, by default, ignores many annoying things that tend to get in the way nowadays, such [...]]]></description>
			<content:encoded><![CDATA[<p>By misspelling awk during a search, I&#8217;ve come to know a very useful utility: <a href="http://petdance.com/ack/" target="_blank">ack</a>. I wholeheartedly suggest to install it and use it as a drop in replacement for grep in most circumstances.</p>
<p>Basically, it&#8217;s a developer-friendly grep that, by default, <strong>ignores many annoying things that tend to get in the way nowadays</strong>, such as .svn folders. Other common options, such as recursiveness and &#8220;operate on all files in this directory&#8221; are assumed without the need of explicitly typing them. The output is <strong>structured, colored and highlighted</strong> in a meaningful and useful way &#8212; but it still plays well in a pipe, where it reverts to a more sober, grep-like output. It maintains a small (but extendable) database of filetypes, so that you can ask for specific categories of files to be included or not: for instance &#8211;noshell ignores .sh, .bash, .csh, .tcsh, .ksh, and .zsh files.</p>
<p>It&#8217;s also actively maintained.</p>
<p>It&#8217;s already an indispensable tool for me. Go grab it, you won&#8217;t regret it!</p>
]]></content:encoded>
			<wfw:commentRss>http://unclezeiv.kerid.org/2008/05/use-ack/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

