by mrshoe · 15y ago · 0 comments
class Shape:
def __init__(self, **kwds):
self.shapename = kwds.pop('shapename')
super().__init__(**kwds)
Better written like this? class Shape:
def __init__(self, shapename=None, **kwds):
self.shapename = shapename
super().__init__(**kwds)
It seems like there are some things about Python's which the author doesn't like, and he solves them by adding a lot of boiler plate to each of his classes. It's probably a better idea to just embrace the Python way or use a different language. def __init__(self, shapename, **kwds):
Or, in Python 3: def __init__(self, *, shapename, **kwds):
(making shapename a required keyword-only argument) def __init__(self, shapename, **kwds):
is not better written; it allows shapename to be passed positionally which makes cooperative inheritance fragile. def __init__(self, shapename=None, **kwds):
also allows that. kwds.pop('shapename', None)
Will get you what you're looking for. In [1]: def init(shapename=None, **kwargs):
...: pass
...:
In [2]: init(shapename='circle', **{'shapename': 'circle'})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/teh/Desktop/<ipython console> in <module>()
TypeError: init() got multiple values for keyword argument 'shapename' >>> def init(**kwargs):
... pass
...
>>> init(shapename='circle', **{'shapename': 'circle'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: init() got multiple values for keyword argument 'shapename'