How callbacks return things

I was trying to understand how a variable is updated when we return a dict from a callback to a callback handler. The code is at

the update code is given by

def _call_and_update(self, cb, cb_name, **kwargs)->None:
"Call `cb_name` on `cb` and update the inner state."
new = ifnone(getattr(cb, f'on_{cb_name}')(**self.state_dict, **kwargs), dict())
for k,v in new.items():
     if k not in self.state_dict:
          raise Exception(f"{k} isn't a valid key in the state of the callbacks.")
          else: self.state_dict[k] = v

Firstly, what does

new = ifnone(getattr(cb, f’on_{cb_name}’)(**self.state_dict, **kwargs), dict())

do? I do not know what is ifnone and what the rest even means?
Secondly even after we update the dict from

self.state_dict[k] = v

how is the actual variable changed?

1 Like

Takes 2 args. If the first one is None, it returns the second arg. Otherwise it returns the first. Kinda how you can supply getattr with a default value to return in case the attribute you’re looking for doesn’t exist. That part is simple.

Anyways. The getattr() retrieves the relevant callback function, (‘on_train_begin’, ‘on_epoch_begin’, etc.) then the function gets called with the state_dict and kwargs. And if it returns a value, it gets put it the variable ‘new’. But if it returns None, ‘new’ gets assigned an empty dict, courtesy of ifnone.

Not sure what you meant by the last part though. state_dict is the variables. So changing state_dict changes the variables.

Thanks. What do you mean state_dict ‘is’ the variables? You mean everytime those variables are called they are called like learner.state_dict[varname]?