I need to be able to pass an extra argument to the __setattr__ method of a child class within a metaclass, but for some reason, the reference to the instance of the child class (self) is not passed in such cases.
However, if I don't override the method from the outside and instead add an additional argument within the class, everything will work fine...
class A:
def __setattr__(self, key, value, extra_keyword=()):
super().__setattr__(key, value)
a = A()
a.a = 1
I need to make one of the commented variants work. Is this possible?
def setattr_(self, key, value, *, extra_keyword=()):
object.__setattr__(self, key, value)
class Setattr:
def __init__(self, *, extra_keyword=()):
self.extra = extra_keyword
def __call__(self, instance, key, value):
object.__setattr__(instance, key, value)
class Metaclass(type):
def __new__(mcs, name, bases, attrs):
attrs["__setattr__"] = setattr_ # works, but I can't pass an extra argument
# !!!
# attrs["__setattr__"] = Setattr(extra_keyword=(1, 2, 3))
# TypeError: __call__() missing 1 required positional argument: 'value'
# attrs["__setattr__"] = functools.partial(setattr_, extra_keyword=(1, 2, 3))
# TypeError: setattr_() missing 1 required positional argument: 'value'
# !!!
return super().__new__(mcs, name, bases, attrs)
class Class(metaclass=Metaclass):
def __init__(self):
self.a = 1
Class()
