Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Does Calling Work In Python?
    primarykey
    data
    text
    <p>For a project I'm working on, I'm implementing a linked-list data-structure, which is based on the idea of a pair, which I define as:</p> <pre><code>class Pair: def __init__(self, name, prefs, score): self.name = name self.score = score self.preferences = prefs self.next_pair = 0 self.prev_pair = 0 </code></pre> <p>where <code>self.next_pair</code> and <code>self.prev_pair</code> are pointers to the previous and next links, respectively. </p> <p>To set up the linked-list, I have an install function that looks like this.</p> <pre><code>def install(i, pair): flag = 0 try: old_pair = pair_array[i] while old_pair.next_pair != 0: if old_pair == pair: #if pair in remainders: remainders.remove(pair) return 0 if old_pair.score &lt; pair.score: flag = 1 if old_pair.prev_pair == 0: # we are at the beginning old_pair.prev_pair = pair pair.next_pair = old_pair pair_array[i] = pair break else: # we are not at the beginning pair.prev_pair = old_pair.prev_pair pair.next_pair = old_pair old_pair.prev_pair = pair pair.prev_pair.next_pair = pair break else: old_pair = old_pair.next_pair if flag==0: if old_pair == pair: #if pair in remainders: remainders.remove(pair) return 0 if old_pair.score &lt; pair.score: if old_pair.prev_pair==0: old_pair.prev_pair = pair pair.next_pair = old_pair pair_array[i] = pair else: pair.prev_pair = old_pair.prev_pair pair.next_pair = old_pair old_pair.prev_pair = pair pair.prev_pair.next_pair = pair else: old_pair.next_pair = pair pair.prev_pair = old_pair except KeyError: pair_array[i] = pair pair.prev_pair = 0 pair.next_pair = 0 </code></pre> <p>Over the course of the program, I am building up a dictionary of these linked-lists, and taking links off of some and adding them in others. Between being pruned and re-installed, the links are stored in an intermediate array.</p> <p>Over the course of debugging this program, I have come to realize that my understanding of the way Python passes arguments to functions is flawed. Consider this test case I wrote:</p> <pre><code>def test_install(): p = Pair(20000, [3, 1, 2, 50], 45) print p.next_pair print p.prev_pair parse_and_get(g) first_run() rat = len(juggler_array)/len(circuit_array) pref_size = get_pref_size() print pref_size print install(3, p) print p.next_pair.name print p.prev_pair </code></pre> <p>When I run this test, I get the following result.</p> <pre><code>0 0 10 None 10108 0 </code></pre> <p>What I don't understand is why the second call to <code>p.next_pair</code> produces a different result (<code>10108</code>) than the first call (<code>0</code>). <code>install</code> does not return a <code>Pair</code> object that can overwrite the one passed in (it returns <code>None</code>), and it's not as though I'm passing <code>install</code> a pointer.</p> <p>My understanding of call-by-value is that the interpreter copies the values passed into a function, leaving the caller's variables unchanged. For example, if I say</p> <pre><code>def foo(x): x = x+1 return x baz = 2 y = foo(baz) print y print baz </code></pre> <p>Then <code>3</code> and <code>2</code> should be printed, respectively. And indeed, when I test that out in the Python interpreter, that's what happens.</p> <p>I'd really appreciate it if anyone can point me in the right direction here. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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