I’m baffled. I’m trying to make a subclass that doesn’t care about any keyword parameters — just passes them all along as is to the superclass, and explicitly sets the one parameter that is required for the constructor. Here’s a simplified version of my code:
class BaseClass(object):
def __init__(self, required, optional=None):
pass
def SubClass(BaseClass):
def __init__(self, **kwargs):
super(SubClass, self).__init__(None, **kwargs)
a = SubClass(optional='foo') # this throws TypeError!?!??
This fails with
leo@loki$ python minimal.py
Traceback (most recent call last):
File "minimal.py", line 9, in <module>
a = SubClass(optional='foo')
TypeError: SubClass() got an unexpected keyword argument 'optional'
How can it complain about an unexpected keyword argument when the method has **kwargs?
(Python 2.7.3 on Ubuntu)
is a function, not a class. There’s no error because
BaseClasscould be an argument name, and nested functions are allowed. Syntax is fun, isn’t it?