I'm trying to extend a method with a single keyword argument while remaining impartial to the rest of the method's signature; I just want to pass that on. Attempt 0:
class SomeSuperclass(object):
pass # in reality: some implementation for some_method
class SomeClass(SomeSuperclass):
def some_method(self, my_kwarg=42, *args, **kwargs):
super(SomeClass, self).some_method(*args, **kwargs)
do_something_interesting_with(my_kwarg)
SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5, my_kwarg=8)
This does not work:
Traceback (most recent call last):
File "test.py", line 9, in <module>
SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5, my_kwarg=8)
TypeError: some_method() got multiple values for keyword argument 'my_kwarg'
I fully understand why the above does not work, but I'm looking for a nice way to make it work. This is my current (ugly) workaround:
class SomeClass(SomeSuperclass):
def some_method(self, *args, **kwargs):
my_kwarg = kwargs.get('my_kwarg', 42)
if 'my_kwarg' in kwargs:
del kwargs['my_kwarg']
super(SomeClass, self).some_method(*args, **kwargs)
do_something_interesting_with(my_kwarg)
That's 3 lines of cruft per kwarg...
Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword. A. A.
Embrace keyword arguments in Python Consider using the * operator to require those arguments be specified as keyword arguments. And remember that you can accept arbitrary keyword arguments to the functions you define and pass arbitrary keyword arguments to the functions you call by using the ** operator.
If you need to write a function that accepts arbitrary keyword arguments, you can use the ** operator in your function definition.
self is an object reference to the object itself, therefore, they are same. Python methods are not called in the context of the object itself. self in Python may be used to deal with custom object models or something.
I'd use dict.pop()
, like so:
class SomeSuperclass(object):
def some_method(self, *args, **kwargs):
print 'SomeSuperclass: args=%s kwargs=%s' % (args, kwargs)
class SomeClass(SomeSuperclass):
def some_method(self, *args, **kwargs):
my_kwarg = kwargs.pop('my_kwarg', 42) # 42 is the default
print 'SomeClass: my_kwarg=%s' % my_kwarg
super(SomeClass, self).some_method(*args, **kwargs)
SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5, my_kwarg=8)
SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5)
When run, this prints out:
SomeClass: my_kwarg=8
SomeSuperclass: args=('arg 0', 'arg 1') kwargs={'some_kwargs': 5}
SomeClass: my_kwarg=42
SomeSuperclass: args=('arg 0', 'arg 1') kwargs={'some_kwargs': 5}
You can use {}.pop()
to turn your 3 lines of cruft into 1:
my_kwarg = kwargs.pop('my_kwarg', 42)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With