<?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>jainbasil&#039;s blog &#187; Python</title>
	<atom:link href="http://jainbasil.net/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://jainbasil.net</link>
	<description>on life, technology and hacks.</description>
	<lastBuildDate>Fri, 20 Jan 2012 20:00:13 +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>Functions as an Argument to Another Function in Python</title>
		<link>http://jainbasil.net/2011/05/functions-as-an-argument-to-another-function-in-python/</link>
		<comments>http://jainbasil.net/2011/05/functions-as-an-argument-to-another-function-in-python/#comments</comments>
		<pubDate>Fri, 06 May 2011 08:16:00 +0000</pubDate>
		<dc:creator>jainbasil</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://jainbasil.net/?p=11</guid>
		<description><![CDATA[Last month, I was interviewed by InfinitelyBeta recruitment team. Interview was really cool, and I really enjoyed every part of it. One of the tasks given to me was to implement a simple function called reduce with the following characteristics: first parameter of reduce should be name of a function, defined earlier. second parameter should [...]]]></description>
			<content:encoded><![CDATA[<p>Last month, I was interviewed by InfinitelyBeta recruitment team. Interview was really cool, and I really enjoyed every part of it. One of the tasks given to me was to implement a simple function called reduce with the following characteristics:</p>
<ul>
<li>first parameter of reduce should be name of a function, defined earlier.</li>
<li>second parameter should be a list of arguments, which is to be passed to the first parameter.</li>
<li>should return the result of executing first parameter</li>
</ul>
<p>for eg:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> a+b
<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
reduce function should called as : reduce(add, [1,2]) and should return 3
'</span><span style="color: #483d8b;">''</span></pre></td></tr></table></div>

<p>Let’s see how to write a function, which will call another function using the parameters specified.</p>
<p>In python, we can write a function as follows:<br />
Container Function</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># container function will call the function func with the parameter args</span>
<span style="color: #ff7700;font-weight:bold;">def</span> container<span style="color: black;">&#40;</span>func, args<span style="color: black;">&#41;</span>:
    func<span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># generic function to print</span>
<span style="color: #ff7700;font-weight:bold;">def</span> printer<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a</pre></td></tr></table></div>

<p>If we call</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">container<span style="color: black;">&#40;</span>printer, <span style="color: #483d8b;">&quot;hello&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>it will print hello. If we try to pass the second parameter as a list, for eg:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">container<span style="color: black;">&#40;</span>printer, <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #483d8b;">&quot;hello&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>,it will just print the same list for you. (Remember that print function will just print the complete list when a list is passed to it as argument).</p>
<p>Now, let’s try something more deeper into this container function. Redefining the container function as follows :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> container<span style="color: black;">&#40;</span>func, <span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;">#line 1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> func<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#line 2</span></pre></td></tr></table></div>

<ul>
<li>RULE 1 : asterisk before args in the first line means to “take the rest of the parameters given and put them in a list called args.”</li>
<li>RULE 2 : asterisk before args in the second line mean “take this list called args and unwrap it into the rest of the parameters.”</li>
</ul>
<p>Now, let’s see what happens:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Python 2.6.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>r261:<span style="color: #000000;">67515</span>, Jun <span style="color: #000000;">24</span> <span style="color: #000000;">2010</span>, <span style="color: #000000;">21</span>:<span style="color: #000000;">47</span>:<span style="color: #000000;">49</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
<span style="color: #7a0874; font-weight: bold;">&#91;</span>GCC 4.2.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Apple Inc. build <span style="color: #000000;">5646</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> on darwin
Type <span style="color: #ff0000;">&quot;help&quot;</span>, <span style="color: #ff0000;">&quot;copyright&quot;</span>, <span style="color: #ff0000;">&quot;credits&quot;</span> or <span style="color: #ff0000;">&quot;license&quot;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">more</span> information.
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> def container<span style="color: #7a0874; font-weight: bold;">&#40;</span>fun, <span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
...     <span style="color: #7a0874; font-weight: bold;">return</span> fun<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>
... 
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> def add<span style="color: #7a0874; font-weight: bold;">&#40;</span>a, b<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
...     <span style="color: #7a0874; font-weight: bold;">return</span> a+b
... 
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> container<span style="color: #7a0874; font-weight: bold;">&#40;</span>add, <span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">3</span></pre></div></div>

<p>Here, we got 3 as output, which is correct. How?</p>
<p>When we called the container function with add, 1, 2 as arguments, 1, 2 where added to the tuple args (see RULE 1). Thus, in container function, func => add and args = [1, 2]. Then, according to RULE 2, the tuple args was expanded into parameters for add and add(1, 2) was called. We could easily find the values in args as:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> def container<span style="color: #7a0874; font-weight: bold;">&#40;</span>func, <span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
...     print args
...
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> container<span style="color: #7a0874; font-weight: bold;">&#40;</span>add, <span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span></pre></div></div>

<p>Now see what happens when we try to pass a list as the argument to second parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> container<span style="color: #7a0874; font-weight: bold;">&#40;</span>add, <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span>, <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
  File <span style="color: #ff0000;">&quot;&quot;</span>, line <span style="color: #000000;">1</span>, <span style="color: #000000; font-weight: bold;">in</span> 
  File <span style="color: #ff0000;">&quot;&quot;</span>, line <span style="color: #000000;">2</span>, <span style="color: #000000; font-weight: bold;">in</span> container
TypeError: add<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> takes exactly <span style="color: #000000;">2</span> arguments <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span> given<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span></pre></div></div>

<p>This time, the entire list [1, 2] was taken as a single argument. We may solve this issue by making a minor change to our container function as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> def container<span style="color: #7a0874; font-weight: bold;">&#40;</span>fun, args<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
...     <span style="color: #7a0874; font-weight: bold;">return</span> fun<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>
... 
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> container<span style="color: #7a0874; font-weight: bold;">&#40;</span>add, <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span></pre></div></div>

<p>Here, we’ve specified that container will take exactly 2 arguments, and the second argument args should be unwraped into a list of parameters.</p>
<p>So, what if we need to call the container function as follows?</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">container<span style="color: black;">&#40;</span>function-name, <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Here we have 3 parameters, in which second and third are lists. We shall define the container function as:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> def container<span style="color: #7a0874; font-weight: bold;">&#40;</span>fun, <span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
...     <span style="color: #7a0874; font-weight: bold;">return</span> fun<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> container<span style="color: #7a0874; font-weight: bold;">&#40;</span>add, <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>, <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">4</span>,<span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span>, <span style="color: #000000;">2</span>, <span style="color: #000000;">4</span>, <span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Now, we have enough background to implement the reduce function. I was asked to do it for the add function, so, I did it as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args<span style="color: black;">&#41;</span>:
    k = args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">2</span>:
        k = fun<span style="color: black;">&#40;</span>arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> k  
    <span style="color: #ff7700;font-weight:bold;">return</span> k + <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Since this was only for add, I was asked to make it generic for any function. After some corrections to it, I changed it as follows:</p>
<p><strong>Reduce function</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args<span style="color: black;">&#41;</span>:
    k = args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">2</span>:
        k = fun<span style="color: black;">&#40;</span>arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> k  
    <span style="color: #ff7700;font-weight:bold;">return</span> fun<span style="color: black;">&#40;</span>k, <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong><br />
Tail recursive implementation</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>==<span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    args.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>, fun<span style="color: black;">&#40;</span>args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,args<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>fun, args <span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Bottom line:<br />
Infinitely Beta recruited me, and received the offer letter 2 days after my interview <img src='http://jainbasil.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jainbasil.net/2011/05/functions-as-an-argument-to-another-function-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

