<?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: Is get lazily evaluated?</title>
	<atom:link href="http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/feed/" rel="self" type="application/rss+xml" />
	<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/</link>
	<description>Yet another code monkey blog.</description>
	<lastBuildDate>Wed, 04 Jan 2012 07:28:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Tiho</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-59738</link>
		<dc:creator>Tiho</dc:creator>
		<pubDate>Sun, 07 Jun 2009 12:13:26 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-59738</guid>
		<description>I don&#039;t know but I think that you wrong.I am notcompetent in this but I think so.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know but I think that you wrong.I am notcompetent in this but I think so.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nes</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57201</link>
		<dc:creator>nes</dc:creator>
		<pubDate>Mon, 26 Jan 2009 20:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57201</guid>
		<description>There must be something wrong with Christian&#039;s timing. I&#039;ve seen posts on the internet where the last example has been found to be pretty slow.</description>
		<content:encoded><![CDATA[<p>There must be something wrong with Christian&#8217;s timing. I&#8217;ve seen posts on the internet where the last example has been found to be pretty slow.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57190</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 26 Jan 2009 07:43:12 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57190</guid>
		<description>Just to expand on Robert Lehmann&#039;s example, you can go one step further and define a subclass that allows functions (or any object with a __call__ method really) or values:

[code]
&gt;&gt;&gt; class newdict(dict):
...     def get(self, key, default_value_or_func):
...         if key in self:
...             return self[key]
...         if hasattr(default_value_or_func, &quot;__call__&quot;):
...             return default_value_or_func()
...         return default_value_or_func
... 
&gt;&gt;&gt; def f():
...     print &quot;f was called&quot;
...     return 7
... 
&gt;&gt;&gt; d = newdict({1:2, 3:4})
&gt;&gt;&gt; d.get(1, f)
2
&gt;&gt;&gt; d.get(42, f)
f was called
7
&gt;&gt;&gt; d.get(&quot;heinz&quot;, 57)
57
&gt;&gt;&gt; 
[/code]</description>
		<content:encoded><![CDATA[<p>Just to expand on Robert Lehmann&#8217;s example, you can go one step further and define a subclass that allows functions (or any object with a __call__ method really) or values:</p>
<div class="codesnip-container" >&gt;&gt;&gt; class newdict(dict):<br />
&#8230;     def get(self, key, default_value_or_func):<br />
&#8230;         if key in self:<br />
&#8230;             return self[key]<br />
&#8230;         if hasattr(default_value_or_func, &#8220;__call__&#8221;):<br />
&#8230;             return default_value_or_func()<br />
&#8230;         return default_value_or_func<br />
&#8230;<br />
&gt;&gt;&gt; def f():<br />
&#8230;     print &#8220;f was called&#8221;<br />
&#8230;     return 7<br />
&#8230;<br />
&gt;&gt;&gt; d = newdict({1:2, 3:4})<br />
&gt;&gt;&gt; d.get(1, f)<br />
2<br />
&gt;&gt;&gt; d.get(42, f)<br />
f was called<br />
7<br />
&gt;&gt;&gt; d.get(&#8220;heinz&#8221;, 57)<br />
57<br />
&gt;&gt;&gt;</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: bookstack</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57187</link>
		<dc:creator>bookstack</dc:creator>
		<pubDate>Mon, 26 Jan 2009 05:16:29 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57187</guid>
		<description>Thanks for all the comments first.

I think Robert Lehmann&#039;s proposal is exactly I try to approach.

And also Christian Wyglendowski&#039;s experiment is quite interesting. At least in C++, try/catch is quite expensive, the compiler needs to generate quite little bit code to guarantee the order of exceptions and that constrains the out-of-order optimization. 

And personally, I think the try/except is abused if the condition is not really &lt;strong&gt;exceptional&lt;/strong&gt;.</description>
		<content:encoded><![CDATA[<p>Thanks for all the comments first.</p>
<p>I think Robert Lehmann&#8217;s proposal is exactly I try to approach.</p>
<p>And also Christian Wyglendowski&#8217;s experiment is quite interesting. At least in C++, try/catch is quite expensive, the compiler needs to generate quite little bit code to guarantee the order of exceptions and that constrains the out-of-order optimization. </p>
<p>And personally, I think the try/except is abused if the condition is not really <strong>exceptional</strong>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Larson</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57182</link>
		<dc:creator>Eric Larson</dc:creator>
		<pubDate>Sun, 25 Jan 2009 23:15:20 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57182</guid>
		<description>In your example, wouldn&#039;t passing in f() call the function immediately? I was under the impression the () effectively calls the function in place.

It is an interesting idea though to create a dict type that does lazy evaluation on the get function. You could make the fallbacks pretty complex if there was an option to take a callable.</description>
		<content:encoded><![CDATA[<p>In your example, wouldn&#8217;t passing in f() call the function immediately? I was under the impression the () effectively calls the function in place.</p>
<p>It is an interesting idea though to create a dict type that does lazy evaluation on the get function. You could make the fallbacks pretty complex if there was an option to take a callable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Wyglendowski</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57180</link>
		<dc:creator>Christian Wyglendowski</dc:creator>
		<pubDate>Sun, 25 Jan 2009 20:15:16 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57180</guid>
		<description>I don&#039;t think that try/except is as expensive as you think.  Some testing in ipython with timeit confirms it:


In [2]: d = {&#039;a&#039;:1}

In [3]: timeit d.get(&#039;a&#039;)
1000000 loops, best of 3: 353 ns per loop

In [4]: timeit &quot;try: d[&#039;a&#039;]\nexcept: 1&quot;
10000000 loops, best of 3: 42.3 ns per loop

In [5]: timeit d.get(&#039;b&#039;, 1)
1000000 loops, best of 3: 377 ns per loop

In [6]: timeit &quot;try: d[&#039;b&#039;]\nexcept: 1&quot;
10000000 loops, best of 3: 42.5 ns per loop


So .get(key, fallback) is more expensive in both cases - where a value for the key exists and where the fallback is used.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think that try/except is as expensive as you think.  Some testing in ipython with timeit confirms it:</p>
<p>In [2]: d = {&#8216;a&#8217;:1}</p>
<p>In [3]: timeit d.get(&#8216;a&#8217;)<br />
1000000 loops, best of 3: 353 ns per loop</p>
<p>In [4]: timeit &#8220;try: d['a']\nexcept: 1&#8243;<br />
10000000 loops, best of 3: 42.3 ns per loop</p>
<p>In [5]: timeit d.get(&#8216;b&#8217;, 1)<br />
1000000 loops, best of 3: 377 ns per loop</p>
<p>In [6]: timeit &#8220;try: d['b']\nexcept: 1&#8243;<br />
10000000 loops, best of 3: 42.5 ns per loop</p>
<p>So .get(key, fallback) is more expensive in both cases &#8211; where a value for the key exists and where the fallback is used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Toothy</title>
		<link>http://kunxi.org/archives/2009/01/is-get-lazily-evaluated/comment-page-1/#comment-57179</link>
		<dc:creator>Toothy</dc:creator>
		<pubDate>Sun, 25 Jan 2009 19:44:07 +0000</pubDate>
		<guid isPermaLink="false">http://kunxi.org/?p=353#comment-57179</guid>
		<description>You don&#039;t understand how the language works in general

When you write f() you are evaluating the f function right there and you will pass the value of it into the get function. This has nothing to do with the get function, you cannot write a function at all where
writing f() in the parameter list would postpone executing f(). 

Your analogy is incorrect too.

What you need to do is write a function that allows you to pass other functions into it, not default values.</description>
		<content:encoded><![CDATA[<p>You don&#8217;t understand how the language works in general</p>
<p>When you write f() you are evaluating the f function right there and you will pass the value of it into the get function. This has nothing to do with the get function, you cannot write a function at all where<br />
writing f() in the parameter list would postpone executing f(). </p>
<p>Your analogy is incorrect too.</p>
<p>What you need to do is write a function that allows you to pass other functions into it, not default values.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

