title: Mutable class variables and the behavior of super().__init__
body: <p>I'm trying to understand why my code is behaving the way it is. Basically I've created a parent and child class in two ways. What I'd like to happen is that on each instance that is created for the two classes, the <strong>init</strong> method will update the count of num_of_emps for <strong>each respective</strong> class. So if I have something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>class Employee:
  
      num_of_emps = 0
  
      def __init__(self, first, last, pay, division):
          self.first = first
          self.last = last
          self.pay = pay
          self.division = division
          Employee.num_of_emps += 1
  
  class Manager(Employee):
  
      num_of_emps = 0
  
      def __init__(self, first, last, pay, division):
          self.first = first
          self.last = last
          self.pay = pay
          self.division = division
          Manager.num_of_emps += 1
  
  emp1 = Employee("Dallas", "Mat", 100000, "Sales")
  emp2 = Employee("Mark", "Doe", 50000, "Marketing")
  emp3 = Employee("Jane", "Doe", 50000, "Advertisement")
  man1 = Manager("Bob", "Ben", 200000, "Marketing")
  man2 = Manager("Ryan", "DS", 200000, "Advertisement")
  
  # Employee.num_of_emps returns 3
  # Manager.num_of_emps returns 2
  </code></pre>
  
  <p>This is what I want. However, if I add a super.<strong>init</strong>() method rather than hard coding the attributes of the Manager class, something else happens:</p>
  
  <pre class="lang-py prettyprint-override"><code>class Employee:
  
      num_of_emps = 0
  
      def __init__(self, first, last, pay, division):
          self.first = first
          self.last = last
          self.pay = pay
          self.division = division
          Employee.num_of_emps += 1
  
  class Manager(Employee):
  
      num_of_emps = 0
  
      def __init__(self, first, last, pay, division):
          super().__init__(first, last, pay, division)
          Manager.num_of_emps += 1
  
  emp1 = Employee("Dallas", "Mat", 100000, "Sales")
  emp2 = Employee("Mark", "Doe", 50000, "Marketing")
  emp3 = Employee("Jane", "Doe", 50000, "Advertisement")
  man1 = Manager("Bob", "Ben", 200000, "Marketing")
  man2 = Manager("Ryan", "DS", 200000, "Advertisement")
  
  # Employee.num_of_emps returns 5
  # Manager.num_of_emps returns 2
  </code></pre>
  
  <p>What's confusing me is the parent classes' num_of_emps is also getting updated each time a manager instance is being created. I know the super().<strong>init</strong> method is causing this but can someone explain why?</p>