<?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: Common Mistake about Python default parameter value</title>
	<atom:link href="http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/</link>
	<description>Yet another trial to make a difference!</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:24:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Joni Wilmoth</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-19561</link>
		<dc:creator>Joni Wilmoth</dc:creator>
		<pubDate>Sat, 06 Mar 2010 23:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-19561</guid>
		<description>This is great! Thanks for this post. I am starting development and this got me straight.</description>
		<content:encoded><![CDATA[<p>This is great! Thanks for this post. I am starting development and this got me straight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas Trangez</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13455</link>
		<dc:creator>Nicolas Trangez</dc:creator>
		<pubDate>Sat, 03 Oct 2009 10:46:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13455</guid>
		<description>It&#039;s pretty much like this construct in C/C++/Java/...:
&lt;code&gt;myList = !myList ? [] : myList&lt;/code&gt;, or the inverse: &lt;code&gt;myList = myList ? myList : []&lt;/code&gt;

Or:
&lt;code&gt;
if myList is None:
    myList = []
# No need for an else case, this would be &quot;myList = myList&quot; anyway
&lt;/code&gt;

I think the Python-form is slightly better to read if you put the &#039;most common case&#039; as truth value of the predicate. This makes sure the &#039;most common value&#039; will be immediately visible to the reader, on the right of the assignment operator.</description>
		<content:encoded><![CDATA[<p>It&#8217;s pretty much like this construct in C/C++/Java/&#8230;:<br />
<code>myList = !myList ? [] : myList</code>, or the inverse: <code>myList = myList ? myList : []</code></p>
<p>Or:<br />
<code><br />
if myList is None:<br />
    myList = []<br />
# No need for an else case, this would be "myList = myList" anyway<br />
</code></p>
<p>I think the Python-form is slightly better to read if you put the &#8216;most common case&#8217; as truth value of the predicate. This makes sure the &#8216;most common value&#8217; will be immediately visible to the reader, on the right of the assignment operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas Trangez</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13454</link>
		<dc:creator>Nicolas Trangez</dc:creator>
		<pubDate>Sat, 03 Oct 2009 10:42:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13454</guid>
		<description>The &#039;or&#039; thing comes from the days when &lt;code&gt;A if PRED else B&lt;/code&gt; was not part of the language (it was introduced in Python 2.5 is I&#039;m not mistaken) :-)

Similar:
&lt;code&gt;&gt;&gt;&gt; True and 1 or 2
1
&gt;&gt;&gt; False and 1 or 2
2&lt;/code&gt;

This construct has a major catch though:
&lt;code&gt;&gt;&gt;&gt; False and 0 or 1
1
&gt;&gt;&gt; True and 0 or 1
1&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>The &#8216;or&#8217; thing comes from the days when <code>A if PRED else B</code> was not part of the language (it was introduced in Python 2.5 is I&#8217;m not mistaken) <img src='http://www.ahmedsoliman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Similar:<br />
<code>&gt;&gt;&gt; True and 1 or 2<br />
1<br />
&gt;&gt;&gt; False and 1 or 2<br />
2</code></p>
<p>This construct has a major catch though:<br />
<code>&gt;&gt;&gt; False and 0 or 1<br />
1<br />
&gt;&gt;&gt; True and 0 or 1<br />
1</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: amal</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13428</link>
		<dc:creator>amal</dc:creator>
		<pubDate>Fri, 02 Oct 2009 07:09:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13428</guid>
		<description>thanks
I faced the same problem before
and it took time to understand the problem</description>
		<content:encoded><![CDATA[<p>thanks<br />
I faced the same problem before<br />
and it took time to understand the problem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ahmed S. Farghal</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13427</link>
		<dc:creator>Ahmed S. Farghal</dc:creator>
		<pubDate>Fri, 02 Oct 2009 06:41:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13427</guid>
		<description>it&#039;s like ternary operator in C of Java, myList will be assigned a new empty list ( [] ) if the passed myList was set to None, but if it has a value it&#039;ll be pointing to that value.</description>
		<content:encoded><![CDATA[<p>it&#8217;s like ternary operator in C of Java, myList will be assigned a new empty list ( [] ) if the passed myList was set to None, but if it has a value it&#8217;ll be pointing to that value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Medhat Dawoud</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13420</link>
		<dc:creator>Medhat Dawoud</dc:creator>
		<pubDate>Fri, 02 Oct 2009 01:49:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13420</guid>
		<description>it&#039;s a great point that you show .
but i want to understand this piece of code
&lt;code&gt; myList = [] if myList is None else myList &lt;/code&gt;
can you illustrate it , please ?</description>
		<content:encoded><![CDATA[<p>it&#8217;s a great point that you show .<br />
but i want to understand this piece of code<br />
<code> myList = [] if myList is None else myList </code><br />
can you illustrate it , please ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ahmed S. Farghal</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13413</link>
		<dc:creator>Ahmed S. Farghal</dc:creator>
		<pubDate>Thu, 01 Oct 2009 18:19:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13413</guid>
		<description>Yes, this makes:
&lt;code&gt; myList = [] if myList is None else myList&lt;/code&gt;
better because it won&#039;t fail in your second scenario, good point ;)</description>
		<content:encoded><![CDATA[<p>Yes, this makes:<br />
<code> myList = [] if myList is None else myList</code><br />
better because it won&#8217;t fail in your second scenario, good point <img src='http://www.ahmedsoliman.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas Trangez</title>
		<link>http://www.ahmedsoliman.com/2009/10/01/common-mistake-about-python-default-parameter-value/comment-page-1/#comment-13412</link>
		<dc:creator>Nicolas Trangez</dc:creator>
		<pubDate>Thu, 01 Oct 2009 18:16:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.ahmedsoliman.com/?p=483#comment-13412</guid>
		<description>A common pattern is
&lt;code&gt;def f(a=None):
    a = a or list()&lt;/code&gt;
which is slightly less verbose. Do note this approach does not work in case the function behaviour would be a modification of the input list, like
&lt;code&gt;def f(a=None):
    a = a or list()
    for i in xrange(len(a)):
        a[i] = a[i] + 1
    return a

b1 = [1, 2, 3]
assert f(b1) is b1

b2 = []
# This fails
assert f(b2) is b2&lt;/code&gt;

But obviously that&#039;s in first place extremely bad/stupid API design.</description>
		<content:encoded><![CDATA[<p>A common pattern is<br />
<code>def f(a=None):<br />
    a = a or list()</code><br />
which is slightly less verbose. Do note this approach does not work in case the function behaviour would be a modification of the input list, like<br />
<code>def f(a=None):<br />
    a = a or list()<br />
    for i in xrange(len(a)):<br />
        a[i] = a[i] + 1<br />
    return a</p>
<p>b1 = [1, 2, 3]<br />
assert f(b1) is b1</p>
<p>b2 = []<br />
# This fails<br />
assert f(b2) is b2</code></p>
<p>But obviously that&#8217;s in first place extremely bad/stupid API design.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
