Got `UnboundLocalError` error when measuring time for `torch.where`
When try to %timeit in Jupyter Notebook got error; Without it working fine.


  UnboundLocalError: local variable 'a' referenced before assignment


import torch
a = torch.rand(10)
b = torch.rand(10)


%timeit a = torch.where(b &gt; 0.5, torch.tensor(0.), a)


What is happening here?

At first, I thought that's because %timeit only evaluates the time run by functions. But thanks to @Shiva who told me it can calculate the execution time of other things. And I checked the documentation here, and I found out this is true.

So, according to this answer, %timeit has a problem with re-assignment as the re-assignment to a causes the function to have an a local variable, hiding the global. In other words, you can use any other variable other that a to assign it to torch.where:

#this works
%timeit c = torch.where(b &gt; 0.5, torch.tensor(0.), a) #c instead of a

# this works
%timeit torch.where(b &gt; 0.5, torch.tensor(0.), a)

# this doesn't work
%timeit a = torch.where(b &gt; 0.5, torch.tensor(0.), a)

