Python property getter and setter with nn.Module

username: reallxc
cooked: <p>In the code below, I’m using property getter and setter to assign a <code>Module</code> (<code>SubNet</code>) to an attribute of another <code>Module</code> (<code>Network</code>). It seems that the setter of <code>Network</code> is not called. Not like I must write the code in this way, but I’m wondering why this is prevent.</p>
<pre><code class="lang-auto">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"
</code></pre>

username: doublecc161
cooked: <p>Is stumbled upon the same issue and found an answer here on Stack Overflow:</p><aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/61116433/maybe-i-found-something-strange-on-pytorch-which-result-in-property-setter-not" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/13219126/weiming-xiong" target="_blank" rel="noopener nofollow ugc">
    <img alt="Weiming Xiong" src="https://lh4.googleusercontent.com/-DrRLkFSY0pc/AAAAAAAAAAI/AAAAAAAAAAA/AAKWJJPDFzE3e2FzSrbnh4QL5SnwlQpuBw/photo.jpg?sz=128" class="thumbnail onebox-avatar" width="128" height="128">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/61116433/maybe-i-found-something-strange-on-pytorch-which-result-in-property-setter-not" target="_blank" rel="noopener nofollow ugc">Maybe I found something strange on pytorch, which result in property setter not working</a>
</h4>

<div class="tags">
  <strong>python, pytorch, setter</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/13219126/weiming-xiong" target="_blank" rel="noopener nofollow ugc">
    Weiming Xiong
  </a>
  on <a href="https://stackoverflow.com/questions/61116433/maybe-i-found-something-strange-on-pytorch-which-result-in-property-setter-not" target="_blank" rel="noopener nofollow ugc">07:57AM - 09 Apr 20 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>As far as I understand this is the basic cause: Since <code>nn.Module</code> implements a <code>__settatr__()</code> method that doesn’t call the builtin <code>__settattr__</code> in the case that the attribute you try to set is a itself a <code>nn.Module</code> the property setter is never executed. For other attributes that are not some PyTorch specific types (check the implementation for details: <a href="https://github.com/pytorch/pytorch/blob/eab59bae15bda508cdfbbc69a1a761b2b2c3780b/torch/nn/modules/module.py#L1133-L1178" class="inline-onebox" rel="noopener nofollow ugc">pytorch/module.py at eab59bae15bda508cdfbbc69a1a761b2b2c3780b · pytorch/pytorch · GitHub</a>) the setter should work as usual.</p>