title: python call-by-reference on primitive types
body: <p>I have experience with the imperative languages(C most often), and that's probably why I am confused in learning python.</p>
  
  <p>I am trying to implement a simple bitwise flag-setter functions. So in C it would be like:</p>
  
  <pre><code>void set_state(int *state, int var, int set)
  {
      (set)? *state |= var : *state &amp;= ~var
  }
  
  int is_state(int *state, int var)
  {
      return (*state &amp; var &gt; 0)
  }
  </code></pre>
  
  <p>int *state refers to a state flag that I keep track of, and var is the flag that I would like to set or clear(decided by int set).</p>
  
  <p>So I tried to do the same with Python 3.2 and...</p>
  
  <pre><code>def setState(nState, nVar, bSet):
      if bSet:
          nState |= nVar
      else:
          nState &amp;= ~var
      print(nState)
  </code></pre>
  
  <p>Then I ran...</p>
  
  <pre><code>&gt;&gt;&gt; state
  1024
  &gt;&gt;&gt; nfirst
  1
  &gt;&gt;&gt; nsecond
  2
  &gt;&gt;&gt; setState(state, nfirst, True)
  1025
  &gt;&gt;&gt; state
  1024
  </code></pre>
  
  <p>debugger tells me that the value of 'state' got copied into nState and now nState got mutated itself(meaning it was changed in its own local scope). Wasn't python all about objects and everything-is-call-by-reference?</p>
  
  <p>If so, why can't I see a side effect?</p>
  
  <p>I am lost. Can someone explain what is going on here.</p>