title: Class constructors and keyword arguments - How does Python determine which one is unexpected?
body: <p>Say I define the following class:</p>
  
  <pre><code>class MyClass(object):
      def __init__(self, x, y):
          self.x = x
          self.y = y
  </code></pre>
  
  <p>Normally, one would instantiate this class in one of the following ways:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(1,2)
  &lt;__main__.MyClass object at 0x8acbf8c&gt;
  &gt;&gt;&gt; MyClass(1, y=2)
  &lt;__main__.MyClass object at 0x8acbeac&gt;
  &gt;&gt;&gt; MyClass(x=1, y=2)
  &lt;__main__.MyClass object at 0x8acbf8c&gt;
  &gt;&gt;&gt; MyClass(y=2, x=1)
  &lt;__main__.MyClass object at 0x8acbeac&gt;
  </code></pre>
  
  <p>Which is just fine and dandy.</p>
  
  <p>Now, we try with an invalid keyword argument and see what happens:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(x=1, j=2)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'j'
  </code></pre>
  
  <p>Python correctly raises a type error and complains about the <code>unexpected keyword argument 'j'</code>.</p>
  
  <p>Now, we can try with two invalid keyword arguments:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(i=1,j=2)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  </code></pre>
  
  <p>Notice, that two of the keyword arguments were invalid, but Python is only complaining about one of them, <code>'i'</code> in this case.</p>
  
  <p>Lets reverse the order of the invalid keyword arguments:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(j=2, i=1)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  </code></pre>
  
  <p>That is interesting. I changed the order of the invalid keyword arguments, but Python still decides to complain about <code>'i'</code> and not <code>'j'</code>. So Python obviously doesn't simply pick the first invalid key to complain about.</p>
  
  <p>Lets try some more:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(c=2, i=1)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  &gt;&gt;&gt; MyClass(q=2, i=1)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  </code></pre>
  
  <p>Alphabetically, I tried a letter before <code>i</code> and one after <code>i</code>, so Python is not complaining alphabetically.</p>
  
  <p>Here are some more, this time with <code>i</code> in the first position:</p>
  
  <pre><code>&gt;&gt;&gt; MyClass(i=1, j=2)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  &gt;&gt;&gt; MyClass(i=1, b=2)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'i'
  &gt;&gt;&gt; MyClass(i=1, a=2)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: __init__() got an unexpected keyword argument 'a'
  </code></pre>
  
  <p>Aha! I got it to complain about <code>'a'</code> instead of <code>'i'</code>.</p>
  
  <p>My question is, when invalid keyword arguments are given to a class constructor, how does Python determine which one to complain about?</p>