Question
How do you procedurally create a function in Python which takes specific named arguments but allow those argument names to be data-driven?
Example
Say, you want to create a class decorator, with_init, which adds an __init__ method with specific named arguments such that the following two classes are equivalent.
class C1(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
@with_init('x y z')
class C2(object):
pass
My first attempt cheats by making a function which accepts *args instead of the specific named parameters:
class with_init(object):
def __init__(self, params):
self.params = params.split()
def __call__(self, cls):
def init(cls_self, *args):
for param, value in zip(self.params, args):
setattr(cls_self, param, value)
cls.__init__ = init
return cls
It works in some situations:
>>> C1(1,2,3)
<__main__.C1 object at 0x100c410>
>>> C2(1,2,3)
<__main__.C2 object at 0x100ca70>
But not so much in others:
>>> C2(1,2,3,4) # Should fail, but doesn't.
<__main__.C2 object at 0x100cc90>
>>> C2(x=1, y=2, z=3) # Should succeed, but doesn't.
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
TypeError: init() got an unexpected keyword argument 'y'
Of course I can add code to the nested init function to try and check for every possible situation, but it seems like there should be an easier way.
I've noticed that collections.namedtuple avoids these issues by making a string to pass to exec. That seems very round-about to me, but perhaps that's the solution.
What is the correct implementation of with_init.__call__?
Note: I'd like a Python 2.x solution please.
2 Answers 2
Very roughly. This accepts kw args and checks to see that the number of args is correct
def __call__(self, cls):
def init(cls_self, *args, **kw):
if len(args)+len(kw) != len(self.params):
raise RuntimeError("Wrong number of arguments")
for param, value in zip(self.params, args):
setattr(cls_self, param, value)
vars(cls_self).update(kw)
cls.__init__ = init
return cls
This version has a few improvements
def __call__(self, cls):
def init(cls_self, *args, **kw):
for param, value in zip(self.params, args):
if param in kw:
raise TypeError("Multiple values for %s"%param)
kw[param]=value
if len(args) > len(self.params) or set(kw) != set(self.params):
raise TypeError("Wrong number of arguments")
vars(cls_self).update(kw)
cls.__init__ = init
return cls
This version also tells you about unexpected keyword args
def __call__(self, cls):
def init(cls_self, *args, **kw):
for param, value in zip(self.params, args):
if param in kw:
raise TypeError("Multiple values for %s"%param)
kw[param]=value
unexpected_args = list(set(kw)-set(self.params))
if unexpected_args:
raise TypeError("Unexpected args %s"%unexpected_args)
missing_args = list(set(self.params)-set(kw))
if missing_args:
raise TypeError("Expected args %s"%missing_args)
vars(cls_self).update(kw)
cls.__init__ = init
return cls
4 Comments
*args and **kw.TypeError: Wrong number of arguments to TypeError: __init__() got an unexpected keyword argument 'w'.exec :)Here's my namedtuple inspired answer. It's currently open to template injection attacks but you don't have to handle any of the parameter errors yourself.
def __call__(self, cls):
paramtxt = ', '.join(['self'] + self.params)
bodytxt = '\n\t'.join('self.%(param)s = %(param)s' % locals() for param in self.params)
template = 'def __init__(%(paramtxt)s):\n\t%(bodytxt)s' % locals()
namespace = dict()
exec template in namespace
cls.__init__ = namespace['__init__']
return cls
1 Comment
exec is open to injection attacks? Are you creating these classes from hostile user input? If you are just using them with hardcoded paramtxt/bodytxt there is no problem