Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Functions which has default parameter(s)
    primarykey
    data
    text
    <p>I have some question about functions which have default parameters. <br /></p> <pre><code>import sys from random import randint my_list = [1,2,3] def Veli(a, b = my_list): my_list.append(randint(1,1500)) print a + b[-1] print "Number of Refs:", sys.getrefcount(my_list) print "Def args:", Veli.func_defaults Veli(1) # This is 4 Veli(1) # We almost always obtain different result because of randint Veli(1) # Again different results. print Veli.func_defaults print "Number of Refs:", sys.getrefcount(my_list) Veli(1) my_list = [-5] # old my_list has different address print "Number of Refs:", sys.getrefcount(my_list) # Less than previous print "Def args:", Veli.func_defaults Veli(1) # Now gives same results. print "Def args:", Veli.func_defaults Veli(1) # Same result again... </code></pre> <p><br /> Outputs: (Some numbers depend on which values randint has returned, of course.) <br /> <blockquote></p> <p>Number of Refs: 3<br /> Def args: ([1, 2, 3],)<br /> 322<br /> 1119<br /> 740<br /> ([1, 2, 3, 321, 1118, 739],)<br /> Number of Refs: 3<br /> 303<br /> Number of Refs: 2<br /> Def args: ([1, 2, 3, 321, 1118, 739, 302],)<br /> 303<br /> Def args: ([1, 2, 3, 321, 1118, 739, 302],)<br /> 303<br /> </blockquote></p> <p></p> <p><br /> The following code gives you a number less than the previous one, because b and my_list is not referenced to same address anymore.</p> <pre><code>print "#ref:", sys.getrefcount(my_list) # Less than previous </code></pre> <p>Now, we have a way (the only way?) to reach b, default argument of the function:</p> <pre><code>Veli.func_defaults[0] </code></pre> <p><br /> Sorry about my long explanation. Here is my questions:</p> <ol> <li><p>Is this a problem? Dictionary of my module crush my_list variable then now my_list variable has default address than previous. Then function which uses global variable named my_list in its body is changing (growing) while my default argument is not. Why cannot <code>b</code> see global variable named <code>my_list</code> when <code>a + b[-1]</code> expression is executed? I know, <code>b</code> has different address with my_list (because <a href="http://docs.python.org/reference/datamodel.html#objects-values-and-types" rel="nofollow">mutual objects (like list) are guaranteed to refer to different, unique, newly created list</a>) now, but why were Python implemented so that <code>b</code> cannot see the global variables when b is a function arguments? Could you explain comprehensively?</p></li> <li><p>Is there any way to get same results with Veli.func_defaults[0]? This code executed then let say, I want to change my default arguments of my function named Veli. I cannot do this with my_list, because my_list and b have different addresses. One way is changing the elements of the list, Veli.func_defaults[0]. Is there any different way(s)?</p></li> <li><p>(It is not so related to code above) How can get addresses of variable? For example, how can get address of <code>b</code>? I use built-in function such as <code>__hash__</code> , but there should be more appropriate way.</p></li> </ol> <p><br /> Notes:</p> <p>a) This codes may be useless for any reason, but I want to learn opinions.<br/> b) Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)<br/> [GCC 4.4.5] on linux2 </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload