4 ms·
self.mocks = dict(zip(args[::2], args[1::2])) Makes for a very unpythonic feeling API. I’d be interested to hear why the author settled on this design, and see
by jacobolus 15y ago
self.mocks = dict(zip(args[::2], args[1::2]))
Makes for a very unpythonic feeling API. I’d be interested to hear why the author settled on this design, and see example uses that he likes the look of. Are there any other libraries which use this pattern?
- tebeka 15y agoYou can either use mock(obj, {'x': 1, 'y': 2}) or mock(obj, 'x', 1, 'y', 2). I thought it'll be nice to provide both options, but you might be right and using just a dict of mocks is more Pythonic.
- MBlume 15y agoAt risk of bikeshedding, I think the "most pythonic" approach would be mock(obj, x=1, y=2) Seriously though, cool lib, thanks for it =)
- jacobolus 15y agoYes, the most pythonic is: mock(obj, x=1, y=2) Which you get by: def __init__(self, obj, **mocks): self.obj = obj self.mocks = mocks This can then also be called (using the built-in double-star-unpacking feature) as: mock(obj, **{'x': 1, 'y': 2}) or even mock(obj, **dict([('x', 1), ('y', 2)])) Then there’s no explicit need to support multiple alternative function signatures, and in particular you don’t get people using the (quite atypical and therefore semantically ambiguous): mock(obj, 'x', 1, 'y', 2).
- tebeka 15y agoThanks, updated the code to use keyword arguments only.