In the code below, I’m using property getter and setter to assign a Module
(SubNet
) to an attribute of another Module
(Network
). It seems that the setter of Network
is not called. Not like I must write the code in this way, but I’m wondering why this is prevent.
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self._sub_net = None
@property
def sub_net(self):
print("getter")
return self._sub_net
@sub_net.setter
def sub_net(self, net):
print("setter")
self._sub_net = net
class SubNet(nn.Module):
def __init__(self):
super(SubNet, self).__init__()
pass
class Object(object):
def __init__(self):
super(Object, self).__init__()
pass
ss = Network()
ss.sub_net = SubNet() # this prints nothing
# ss.sub_net = Object() # this prints "setter"