quoting from docs:http://docs.python.org/reference/compound_stmts.html
*Default parameter values are evaluated when the function definition is executed.* 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 so default object for x in foo is precomputed once and is modified with each call. hth --shashank On Thu, Mar 11, 2010 at 3:02 PM, jitendra gupta <jitu.ic...@gmail.com>wrote: > > def foo(x = [0]): > x[0] = x[0] + 1 > return x[0] > > def soo(x = None): > if x is None: > x = [0] > x[0] = x[0] + 1 > return x[0] > > >>> foo() > 1 > >>>foo() #See the behavior incremented by one > 2 > >>>foo([1]) # but here based on given number > 2 > >>>foo() > 3 > >>>foo([1]) > 2 > >>>foo() > 4 > > >>>soo() > 1 > >>>soo() > 1 > >>>soo([1]) > 2 > >>>soo() > 1 > > Why foo() is incremented by 1 always when we are not passing any argument, > but this is not happening in soo() case, In which scenario we will use > these type of function.' > > Thanks > Jitendra Kumar > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Regards Shashank Singh Senior Undergraduate, Department of Computer Science and Engineering Indian Institute of Technology Bombay shashank.sunny.si...@gmail.com http://www.cse.iitb.ac.in/~shashanksingh
-- http://mail.python.org/mailman/listinfo/python-list