title: Python, how come we can create class variables that were not defined in class creation?
body: <p>Let's say we have this simple Python code</p>
  
  <pre><code>class MyClass(object):
      class_var = 1
  
      def __init__(self, i_var):
          self.i_var = i_var
  </code></pre>
  
  <p>Correct me if I get any of this wrong:</p>
  
  <p>Class_Var is a class variable that is the same for all instances of MyClass object.
  I_Var is an instance variable that only exists in instances of the MyClass object</p>
  
  <pre><code>foo = MyClass(2)
  bar = MyClass(3)
  
  foo.class_var, foo.i_var
  ## 1, 2
  bar.class_var, bar.i_var
  ## 1, 3
  </code></pre>
  
  <p>Class variables are also properties of the class itself.</p>
  
  <pre><code>MyClass.class_var ##
  ## 1
  </code></pre>
  
  <p>MyClass.I_var should error out, correct?</p>
  
  <p><strong>Does that mean that class variables can be considered like instance variables of the class object itself (since all classes are objects) ?</strong></p>
  
  <pre><code>MyClass.new_attribute = 'foo'
  print(hasattr(ObjectCreator, 'new_attribute'))
  </code></pre>
  
  <p>That should return true. And </p>
  
  <pre><code>print (MyClass.new_attribute)
  </code></pre>
  
  <p>should return foo.</p>
  
  <p>How come we can create a new class variable that was not defined in the original definition for that class?  </p>
  
  <p>Is</p>
  
  <pre><code>MyClass.new_attribute = 'foo'
  </code></pre>
  
  <p>the exact same thing as creating that class attribute in the original definition?</p>
  
  <pre><code>class MyClass(object):
      class_var = 1
      new_attribute = 'foo'
  </code></pre>
  
  <p>So we can create new class attributes at runtime? How does that not interfere with the init constructor that creates the class object and has those class variables as instance variables of the class object? </p>