Model attribute not updated correctly

I want the foward( ) have two modes depend on the self.last_only

Now the issue is self.last_only is NOT updated according to my will.

class Model(nn.Module):

    def __init__(self)
        self.last_only = None  

    def forward(self, x1, x2):
        print('self.last_only = ', self.last_only)
def last_only(model, log=print):
    model.last_only = True
    print('model.last_only = ', model.last_only)

model(x1, x2)
It prints out None.

Hi Erica -

You may be looking for something like this:

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.last_only = None

    def forward(self, x1, x2):        
        print('self.last_only = ', self.last_only)
        return None

model = Model()
print("before making the change")
model("x", "y")

model.last_only = True
print("after making the change")
model("x", "y")

Output:
before making the change
self.last_only =  None
after making the change
self.last_only =  True

Thank you, Andrei. It really helps!