Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a keyword argument to an overridden method and using **kwarg

Tags:

python

I am subclassing an object in order to override a method that I want to add some functionality to. I don't want to completely replace it or add a differently named method but remain compatible to the superclasses method by just adding an optional argument to the method. Is it possible to work with *args and **kwargs to pass through all arguments to the superclass and still add an optional argument with a default? I intuitively came up with the following but it doesn't work:

class A(object):
    def foo(self, arg1, arg2, argopt1="bar"):
        print arg1, arg2, argopt1

class B(A):
    def foo(self, *args, argopt2="foo", **kwargs):
        print argopt2
        A.foo(self, *args, **kwargs)


b = B()
b.foo("a", "b", argopt2="foo")

Of course I can get it to work when I explicitly add all the arguments of the method of the superclass:

class B(A):
    def foo(self, arg1, arg2, argopt1="foo", argopt2="bar"):
        print argopt2
        A.foo(self, arg1, arg2, argopt1=argopt1)

What's the right way to do this, do I have to know and explicitly state all of the overridden methods arguments?

like image 732
hawk64 Avatar asked Nov 11 '09 15:11

hawk64


People also ask

What does Kwarg mean?

**kwargs stands for keyword arguments. The only difference from args is that it uses keywords and returns the values in the form of a dictionary. Now, let's write a normal function and pass arguments through args and kwargs.

How do I set 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.

What is the correct way to use the Kwargs statement?

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks ( ** ) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

How do you create a positional argument in Python?

Positional Arguments in Python A positional argument in Python is an argument whose position matters in a function call. Let's define a function that shows info about a person given the name and age: def info(name, age): print(f"Hi, my name is {name}.


1 Answers

class A(object):
    def foo(self, arg1, arg2, argopt1="bar"):
        print arg1, arg2, argopt1

class B(A):
    def foo(self, *args, **kwargs):
        argopt2 = kwargs.get('argopt2', default_for_argopt2)
        # remove the extra arg so the base class doesn't complain. 
        del kwargs['argopt2']
        print argopt2
        A.foo(self, *args, **kwargs)


b = B()
b.foo("a", "b", argopt2="foo")
like image 83
Ned Batchelder Avatar answered Nov 15 '22 13:11

Ned Batchelder