title: How to define a Python class which accepts type parameters
body: <p>Python 3.5 introduced <em>type hints</em> which allow one to write the following:</p>
  
  <pre><code>from typing import Union
  
  answer: Union[int, str] = 42
  answer = '42'
  </code></pre>
  
  <p>documentation: <a href="https://docs.python.org/3/library/typing.html#typing.Union" rel="nofollow noreferrer">https://docs.python.org/3/library/typing.html#typing.Union</a></p>
  
  <p>I think I understand the naive meaning of the above code. In particular, it means that the variable <code>answer</code> has been given a type hint, which says that it is supposed to be of the <code>Union</code> type with type parameters <code>int</code> and <code>str</code>, which in turn means that it is supposed to be either <code>int</code> or <code>str</code>.</p>
  
  <p>What I do not understand, however, are the formal Python language rules around defining and using classes with type parameters in square brackets.</p>
  
  <p>Can someone explain it?</p>