Maybe I found something strange on pytorch, which result in property setter not working. Below is a minimal example that demonstrates this:
import torch.nn as nn
class A(nn.Module):
def __init__(self):
super(A, self).__init__()
self.aa = 1
self.oobj = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@property
def obj(self):
print('get attr [obj]: {0}'.format(self.oobj))
return self.oobj
@obj.setter
def obj(self, val):
print('set attr [obj] to {0}'.format(val))
self.oobj = val
class B(nn.Module):
def get_attr(self):
print('no any attr.')
class C:
def get_attr(self):
print('no any attr.')
b = A() # set obj, and prints my setter message
b.obj # get obj using my getter
# respectively run the following 3 lines, only the last line not call the setter I defined explicitly.
b.obj = C() # set obj, and prints my setter message
# b.obj = [1, 2, 3] # set obj, and prints my setter message
# b.obj = B() # set obj, but it doesn't print my setter message
The last line doesn't call property setter I defined on class A, but call setter on torch.nn.Module. Because A regard B as a nn.Module, call the setter on nn.Module to set attr [obj] as a Module, but it still strange, why not call the setter I explicitly defined on class A?