title: Class attributes and their initialization
body: <p>I'm quite new in python and during these days I'm exploring classes. I have a question concerning attributes and variables inside classes: What is the difference between defining an attribute via just <code>q=1</code> in the body of the class and via defining <code>self.q=1</code> inside the <code>__init__</code>? For example, what is the difference between the following two possibilities? </p>
  
  <pre><code>class MyClass1:
      q=1
      def __init__(self,p):
          self.p=p
      def AddSomething(self,x):
          self.q = self.q+x
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class MyClass2:
      def __init__(self,p):
          self.q=1
          self.p=p
      def AddSomething(self,x):
          self.q = self.q+x
  </code></pre>
  
  <p>The output of for example:</p>
  
  <pre><code>&gt;&gt;&gt; my=MyClass1(2)
  &gt;&gt;&gt; my.p
  2
  &gt;&gt;&gt; my.q
  1
  &gt;&gt;&gt; my.AddSomething(7)
  &gt;&gt;&gt; my.q
  8
  </code></pre>
  
  <p>does not depend on whether <code>MyClass1</code> or <code>MyClass2</code> is used. Neither in <code>MyClass1</code> nor in <code>MyClass2</code> does an error occur. </p>