How to properly add some new arguments in a subclass? I am trying to subclass list in order to implement a callback function that would be called whenever a list item is being set.

I have this:

class CustomList(list):
    def __init__(self, *args, **kwargs):
        self.setitem_callback = kwargs['setitem_callback']
        del kwargs['setitem_callback']
        super().__init__(*args, **kwargs)

    def __setitem__(self, *args, **kwargs):
        super().__setitem__(*args, **kwargs)
        self.setitem_callback()

def callback():
    print('callback')



 l = ['a', 'b', 'c']
 custom_list = CustomList(l, setitem_callback=callback)
 custom_list[1] = 'd' # prints 'callback'


It works as expected, but I am concerned about having to do del kwargs['setitem_callback'] in order for the superclass' __init__ to work. Is it a bad practice? Are there any other ways to achieve the same?
As @Peilonrayz said in the comments: You can just write

class CustomList(list):
    def __init__(self, *args, setitem_callback, **kwargs):
        self.setitem_callback = setitem_callback
        super().__init__(*args, **kwargs)

    def __setitem__(self, *args, **kwargs):
        super().__setitem__(*args, **kwargs)
        self.setitem_callback()

def callback():
    print('callback')

l = ['a', 'b', 'c']
custom_list = CustomList(l, setitem_callback=callback)
custom_list[1] = 'd' # prints 'callback'


as long as you're okay with slightly different behavior when the caller forgets to pass setitem_callback= at all. With your original code, you get a KeyError:

Traceback (most recent call last):
  File "test.py", line 16, in &lt;module&gt;
    custom_list = CustomList(l)
  File "test.py", line 3, in __init__
    self.setitem_callback = kwargs['setitem_callback']
KeyError: 'setitem_callback'


With a proper keyword argument, you get a TypeError with a more tailored message:

Traceback (most recent call last):
  File "test.py", line 16, in &lt;module&gt;
    custom_list = CustomList(l)
TypeError: __init__() missing 1 required keyword-only argument: 'setitem_callback'