I have a innerclass
decorator/descriptor that is supposed to pass the outer instance to the inner callable as the first argument:
from functools import partial
class innerclass:
def __init__(self, cls):
self.cls = cls
def __get__(self, obj, obj_type=None):
if obj is None:
return self.cls
return partial(self.cls, obj)
Here's a class named Outer
whose .Inner
is a class decorated with innerclass
:
class Outer:
def __init__(self):
self.inner_value = self.Inner('foo')
@innerclass
class Inner:
def __init__(self, outer_instance, value):
self.outer = outer_instance
self.value = value
def __set__(self, outer_instance, value):
print('Setter invoked')
self.value = value
I expected that the setter would be invoked when I change the attribute. However, that is not the case:
foo = Outer()
print(type(foo.inner_value)) # <class '__main__.Outer.Inner'>
foo.inner_value = 42
print(type(foo.inner_value)) # <class 'int'>
Why is that and how can I fix it?