Clean way to disable `__setattr__` until after initialization
A way to disable the __setattr__
until after initialization without changing the self.value = value
syntax in the __init__
method is covered here. In short, embed knowledge of initialization in the object and use it in the __setattr__
method. For your Wrapper
:
class Wrapper:
__initialized = False
def __init__(self, value):
self.value = value
self.__initialized = True
def __setattr__(self, name, value):
if self.__initialized:
# your __setattr__ implementation here
else:
object.__setattr__(self, name, value)
You are catching all assignments, which prevents the constructor from assigning self.value
. You can use self.__dict__
to access the instance dictionary. Try:
class Wrapper:
def __init__(self, value):
self.__dict__['value'] = value
def __setattr__(self, name, value):
setattr(self.value, name, value)
Another way using object.__setattr__
:
class Wrapper(object):
def __init__(self, value):
object.__setattr__(self, 'value', value)
def __setattr__(self, name, value):
setattr(self.value, name, value)