title: Changing class attribute value before and after the instance one
body: <p>I'm trying to figure out how the class/instance variables work in Python.</p>
  <p>Setting aside the obvious facts like mutable objects (like lists) defined as class variables are shared between instances etc., I've messed around with something different. No better way to visualize it than an example:</p>
  <pre class="lang-py prettyprint-override"><code>class Foo:
      x = 0
  
  
  f = Foo()
  print(f&quot;f.x = {f.x}&quot;)
  print(f&quot;id(Foo.x), id(f.x) {id(Foo.x)} {id(f.x)}&quot;)
  
  print(&quot;Setting Foo.x to 5&quot;)
  Foo.x = 5
  print(f&quot;f.x = {f.x}&quot;)
  print(f&quot;id(Foo.x), id(f.x) {id(Foo.x)} {id(f.x)}&quot;)
  
  print(&quot;Setting f.x to 3&quot;)
  f.x = 3
  print(f&quot;f.x = {f.x}&quot;)
  print(f&quot;id(Foo.x), id(f.x) {id(Foo.x)} {id(f.x)}&quot;)
  
  print(&quot;Setting Foo.x to 6&quot;)
  Foo.x = 6
  print(f&quot;f.x = {f.x}&quot;)
  print(f&quot;id(Foo.x), id(f.x) {id(Foo.x)} {id(f.x)}&quot;)
  </code></pre>
  <p>This is the output:</p>
  <pre class="lang-sh prettyprint-override"><code>f.x = 0
  id(Foo.x), id(f.x) 1711594340560 1711594340560
  Setting Foo.x to 5
  f.x = 5
  id(Foo.x), id(f.x) 1711594340720 1711594340720
  Setting f.x to 3
  f.x = 3
  id(Foo.x), id(f.x) 1711594340720 1711594340656
  Setting Foo.x to 6
  f.x = 3
  id(Foo.x), id(f.x) 1711594340752 1711594340656
  </code></pre>
  <p>So let's walk it line by line:</p>
  <ol>
  <li>At first <code>f.x</code> will be <code>0</code>, obviously</li>
  <li>Now setting <code>Foo.x</code> to <code>5</code> changes the <code>f.x</code> value to <code>5</code> - nothing new here, at least for Python devs</li>
  <li><code>f.x</code> is set to 3, therefore <code>f.x</code> value will be <code>3</code>, again correct</li>
  <li>Now <code>Foo.x</code> is being set to <code>6</code>, I would expect <code>f.x</code> to be changed to <code>6</code> as well</li>
  </ol>
  <p>I've printed the <code>id()</code>s of the attributes to see what's going on. So, what happens is, each time <code>x</code> value (on <code>Foo</code> or <code>f</code>) gets changed, it's a new object, new id. I understand that. But why in the case of first <code>Foo.x = 5</code> change (when <code>Foo.x</code> and <code>f.x</code> have the same ids), the attribute gets updated both in class and the instance? This won't happen again, when these are two separate objects.</p>
  <p>I mean the question is this: how does it happen (or how does Python know) that when <code>Foo.x</code> and <code>f.x</code> are the same immutable object, when I reassign <code>Foo.x</code>, both of them will get changed?</p>