Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What is an elegant idiom for extending method with one or more keyword arguments?

Tags:

python

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...

like image 665
Klaas van Schelven Avatar asked Jul 28 '11 12:07

Klaas van Schelven


People also ask

What is a keyword argument in Python?

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.

How do you get keyword arguments in python?

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.

How would you define a function with arbitrary keyword arguments?

If you need to write a function that accepts arbitrary keyword arguments, you can use the ** operator in your function definition.

What is self in Python stack overflow?

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.


2 Answers

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}
like image 60
NPE Avatar answered Oct 19 '22 19:10

NPE


You can use {}.pop() to turn your 3 lines of cruft into 1:

my_kwarg = kwargs.pop('my_kwarg', 42)
like image 1
nmichaels Avatar answered Oct 19 '22 21:10

nmichaels