title: Understanding __init_subclass__
body: <p>I finally upgraded my python version and I was discovering the new features added. Among other things, I was scratching my head around the new <a href="https://docs.python.org/3/reference/datamodel.html#object.__init_subclass__" rel="noreferrer"><code>__init_subclass__</code></a> method. From the docs:</p>
  
  <blockquote>
    <p>This method is called whenever the containing class is subclassed. cls
    is then the new subclass. If defined as a normal instance method, this
    method is implicitly converted to a class method.</p>
  </blockquote>
  
  <p>So I started to playing around with it a little bit, following the example in the docs:</p>
  
  <pre><code>class Philosopher:
      def __init_subclass__(cls, default_name, **kwargs):
          super().__init_subclass__(**kwargs)
          print(f"Called __init_subclass({cls}, {default_name})")
          cls.default_name = default_name
  
  class AustralianPhilosopher(Philosopher, default_name="Bruce"):
      pass
  
  class GermanPhilosopher(Philosopher, default_name="Nietzsche"):
      default_name = "Hegel"
      print("Set name to Hegel")
  
  Bruce = AustralianPhilosopher()
  Mistery = GermanPhilosopher()
  print(Bruce.default_name)
  print(Mistery.default_name)
  </code></pre>
  
  <p>Produces this output:</p>
  
  <pre><code>Called __init_subclass(&lt;class '__main__.AustralianPhilosopher'&gt;, 'Bruce')
  'Set name to Hegel'
  Called __init_subclass(&lt;class '__main__.GermanPhilosopher'&gt;, 'Nietzsche')
  'Bruce'
  'Nietzsche'
  </code></pre>
  
  <p>I understand that this method is called <em>after</em> the subclass definition, but my questions are particularly about the usage of this feature. I read the <a href="https://www.python.org/dev/peps/pep-0487/#new-ways-of-using-classes" rel="noreferrer">PEP 487</a> article as well, but didn't help me much. Where would this method be helpful? Is it for:</p>
  
  <ul>
  <li>the superclass to register the subclasses upon creation?</li>
  <li>forcing the subclass to set a field at definition time?</li>
  </ul>
  
  <p>Also, do I need to understand the <a href="https://docs.python.org/3/reference/datamodel.html#object.__set_name__" rel="noreferrer"><code>__set_name__</code></a> to fully comprehend its usage?</p>