<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Idiomatic Python refactoring: for-else, &#8220;in&#8221; (contains) operator</title>
	<atom:link href="http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/</link>
	<description>__author__ = &#039;Chris Leary&#039;</description>
	<lastBuildDate>Mon, 06 Sep 2010 22:38:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Christopher Leary</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-1044</link>
		<dc:creator>Christopher Leary</dc:creator>
		<pubDate>Sat, 21 Mar 2009 08:33:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-1044</guid>
		<description>@rgz: Yeah, it&#039;s pretty neat that way, but it&#039;s only coincidental. It&#039;s important not to get too bogged down in reads-like-English-ness, or you end up with overly complicated things like Perl&#039;s natural language constructs (i.e. &lt;a href=&quot;http://perldoc.perl.org/perlsyn.html#Statement-Modifiers&quot; rel=&quot;nofollow&quot;&gt;postfixed if/unless statements&lt;/a&gt;) or a strange emphasis on English-like readability, like in this &lt;a href=&#039;http://en.wikipedia.org/wiki/COBOL&#039; rel=&quot;nofollow&quot;&gt;COBOL&lt;/a&gt;  &lt;a href=&#039;http://stackoverflow.com/questions/491971/what-programming-language-is-most-like-natural-language/491995#491995&#039; rel=&quot;nofollow&quot;&gt;example on Stack Overflow&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>@rgz: Yeah, it&#8217;s pretty neat that way, but it&#8217;s only coincidental. It&#8217;s important not to get too bogged down in reads-like-English-ness, or you end up with overly complicated things like Perl&#8217;s natural language constructs (i.e. <a href="http://perldoc.perl.org/perlsyn.html#Statement-Modifiers" rel="nofollow">postfixed if/unless statements</a>) or a strange emphasis on English-like readability, like in this <a href='http://en.wikipedia.org/wiki/COBOL' rel="nofollow">COBOL</a>  <a href='http://stackoverflow.com/questions/491971/what-programming-language-is-most-like-natural-language/491995#491995' rel="nofollow">example on Stack Overflow</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rgz</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-1043</link>
		<dc:creator>rgz</dc:creator>
		<pubDate>Sat, 21 Mar 2009 06:54:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-1043</guid>
		<description>Holy cow! That not just Pythonic, it reads as ok English.
 &quot;If [there&#039;re] choices and value not in choices, raise BadValueError.&quot;</description>
		<content:encoded><![CDATA[<p>Holy cow! That not just Pythonic, it reads as ok English.<br />
 &#8220;If [there're] choices and value not in choices, raise BadValueError.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Leary</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-842</link>
		<dc:creator>Christopher Leary</dc:creator>
		<pubDate>Sat, 20 Dec 2008 02:12:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-842</guid>
		<description>@G: Agreed. If you read carefully you&#039;ll see that the statement was unrelated to the usage of for-else:

&quot;We also gain some efficiency over the original by using the break statement as soon as we find a match&quot;

Thanks for pointing it out more explicitly, though!</description>
		<content:encoded><![CDATA[<p>@G: Agreed. If you read carefully you&#8217;ll see that the statement was unrelated to the usage of for-else:</p>
<p>&#8220;We also gain some efficiency over the original by using the break statement as soon as we find a match&#8221;</p>
<p>Thanks for pointing it out more explicitly, though!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: G</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-824</link>
		<dc:creator>G</dc:creator>
		<pubDate>Sat, 13 Dec 2008 14:07:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-824</guid>
		<description>The note about the forelse being more efficient over break is moot. You could of used break in the first code too</description>
		<content:encoded><![CDATA[<p>The note about the forelse being more efficient over break is moot. You could of used break in the first code too</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Leary</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-143</link>
		<dc:creator>Christopher Leary</dc:creator>
		<pubDate>Mon, 06 Oct 2008 00:09:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-143</guid>
		<description>@Dave Kirby: &lt;tt&gt;value in []&lt;/tt&gt; is always False, since an empty list contains no values. Thus, &lt;tt&gt;value not in []&lt;/tt&gt; is always True. Therefore, your snippet will raise a BadValueError if &lt;tt&gt;choices&lt;/tt&gt; evaluates to False, whereas the original snippet will bypass the check altogether if choices evaluates to False.</description>
		<content:encoded><![CDATA[<p>@Dave Kirby: <tt>value in []</tt> is always False, since an empty list contains no values. Thus, <tt>value not in []</tt> is always True. Therefore, your snippet will raise a BadValueError if <tt>choices</tt> evaluates to False, whereas the original snippet will bypass the check altogether if choices evaluates to False.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Kirby</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-141</link>
		<dc:creator>Dave Kirby</dc:creator>
		<pubDate>Sun, 05 Oct 2008 16:40:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-141</guid>
		<description>how about:

&lt;pre lang=&#039;python&#039;&gt;
if  value not in (choices or []):
    raise BadValueError
&lt;/pre&gt;

If choices is None/False/0 etc then (choices or []) will evaluate to an empty list.  The expression needs to be in brackets since &#039;in&#039; binds more tightly than &#039;or&#039;.

&lt;strong&gt;Admin:&lt;/strong&gt; Fixed code syntax -- correct markup is &quot;pre lang=&#039;python&#039;&quot;.</description>
		<content:encoded><![CDATA[<p>how about:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span>  value <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span>choices <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> BadValueError</pre></div></div>

<p>If choices is None/False/0 etc then (choices or []) will evaluate to an empty list.  The expression needs to be in brackets since &#8216;in&#8217; binds more tightly than &#8216;or&#8217;.</p>
<p><strong>Admin:</strong> Fixed code syntax &#8212; correct markup is &#8220;pre lang=&#8217;python&#8217;&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Leary</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-140</link>
		<dc:creator>Christopher Leary</dc:creator>
		<pubDate>Sun, 05 Oct 2008 05:46:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-140</guid>
		<description>@David: In the original code, if choices is anything that evaluates to False, the lookup isn&#039;t performed. For example, the lookup would have been bypassed if choices were False, None, or even an empty iterable! If choices were None, your snippet would have raised a TypeError, which doesn&#039;t follow the semantics of the original.

You could do something like:

&lt;pre lang=&#039;python&#039;&gt;
try:
    if value not in choices:
        raise BadValueError
except TypeError: # Non-iterables.
    pass
&lt;/pre&gt;

But this also changes the behavior from the original in a subtle way. Empty iterables (like []) will raise a BadValueError here, but would have evaluated to False in the original &quot;if choices&quot; and bypassed the value check altogether.</description>
		<content:encoded><![CDATA[<p>@David: In the original code, if choices is anything that evaluates to False, the lookup isn&#8217;t performed. For example, the lookup would have been bypassed if choices were False, None, or even an empty iterable! If choices were None, your snippet would have raised a TypeError, which doesn&#8217;t follow the semantics of the original.</p>
<p>You could do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> choices:
        <span style="color: #ff7700;font-weight:bold;">raise</span> BadValueError
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">TypeError</span>: <span style="color: #808080; font-style: italic;"># Non-iterables.</span>
    <span style="color: #ff7700;font-weight:bold;">pass</span></pre></div></div>

<p>But this also changes the behavior from the original in a subtle way. Empty iterables (like []) will raise a BadValueError here, but would have evaluated to False in the original &#8220;if choices&#8221; and bypassed the value check altogether.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Buxton</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-139</link>
		<dc:creator>David Buxton</dc:creator>
		<pubDate>Sat, 04 Oct 2008 23:57:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-139</guid>
		<description>Why does one need to test for choices as well as value not in choices? Wouldn&#039;t

if value not in choices:
    raise BadValueError

be sufficient? Or is choices set to a non-iterable when it is empty?</description>
		<content:encoded><![CDATA[<p>Why does one need to test for choices as well as value not in choices? Wouldn&#8217;t</p>
<p>if value not in choices:<br />
    raise BadValueError</p>
<p>be sufficient? Or is choices set to a non-iterable when it is empty?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://blog.cdleary.com/2008/10/idiomatic-python-refactoring-for-else-in-contains-operator/comment-page-1/#comment-137</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Fri, 03 Oct 2008 17:20:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=236#comment-137</guid>
		<description>Nice refactoring :)

I think the original snippet of code comes from the C/C++ mentality (e.g. those who first started programming in C). I still find myself writing code like that, even after being exposed to all kinds of syntactic goodies, because it&#039;s the first thing that comes to mind...

Nice tip on for..else as well, I&#039;ve never used it before but maybe I&#039;ll give it a try now :)</description>
		<content:encoded><![CDATA[<p>Nice refactoring :)</p>
<p>I think the original snippet of code comes from the C/C++ mentality (e.g. those who first started programming in C). I still find myself writing code like that, even after being exposed to all kinds of syntactic goodies, because it&#8217;s the first thing that comes to mind&#8230;</p>
<p>Nice tip on for..else as well, I&#8217;ve never used it before but maybe I&#8217;ll give it a try now :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
