quoting from docs:<a href="http://docs.python.org/reference/compound_stmts.html">http://docs.python.org/reference/compound_stmts.html</a><br><br><strong>Default parameter values are evaluated when the function 
definition is
executed.</strong>  This means that the expression is evaluated once, 
when the function
is defined, and that that same “pre-computed” value is used for each 
call.  This
is especially important to understand when a default parameter is a 
mutable
object, such as a list or a dictionary: if the function modifies the 
object
(e.g. by appending an item to a list), the default value is in effect 
modified<br><br>so default object for x in foo is precomputed once and is modified with each call.<br><br>hth<br>--shashank<br><br><div class="gmail_quote">On Thu, Mar 11, 2010 at 3:02 PM, jitendra gupta <span dir="ltr"><<a href="mailto:jitu.icfai@gmail.com">jitu.icfai@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><br></div><div>def foo(x = [0]):</div><div><span style="white-space: pre;"> </span>x[0] = x[0] + 1</div>

<div><span style="white-space: pre;">     </span>return x[0]</div><div><br>
</div><div><div>def soo(x = None):</div><div><span style="white-space: pre;">     </span>if x is None:</div><div><span style="white-space: pre;">               </span>x = [0]</div><div><span style="white-space: pre;">     </span>x[0] = x[0] + 1</div>


<div><span style="white-space: pre;">     </span>return x[0]</div><div><br></div><div>>>> foo()</div><div>1</div><div>>>>foo()  #See the behavior incremented by one </div><div>2</div><div>
>>>foo([1]) # but here based on given number </div><div>2</div><div>>>>foo() </div><div>3</div><div><div>>>>foo([1])</div><div>2</div><div><div>>>>foo()</div><div>4</div><div><br></div>


<div>>>>soo()</div><div>1</div><div>>>>soo()</div><div>1</div><div>>>>soo([1])</div><div>2</div><div><div>>>>soo()</div><div>1</div><div><br></div><div>Why foo() is incremented by 1 always when we are not passing any argument,</div>


<div> but this is not happening in soo() case, In which scenario we will use these type of  function.'</div><div><br></div><div>Thanks </div><div>Jitendra Kumar</div><div><br></div></div></div></div></div>
<br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Regards<br>Shashank Singh<br>Senior Undergraduate, Department of Computer Science and Engineering<br>Indian Institute of Technology Bombay<br><a href="mailto:shashank.sunny.singh@gmail.com">shashank.sunny.singh@gmail.com</a><br>

<a href="http://www.cse.iitb.ac.in/~shashanksingh">http://www.cse.iitb.ac.in/~shashanksingh</a><br>