I have this piece of code that I am not understanding very well, especially the part with the lambda function without arguments.
class uniform:
def __init__(self,a,b)
self.a=a
self.L=b-a
def __call__(self,randfun):
f=lambda : self.a+self.L*randfun()
return f
@uniform(-1,1)
def gen():
from random import random
return random()
#print 5 random numbers
for _ in range(5):
print(gen())
If I remove that lambda resulting in f=self.a+self.L*randfun()
I get this type of error: Exception has occurred: TypeError 'float' object is not callable
. Why is this happenning? Same thing if I do return self.a+self.L*randfun()
but nothing happens if I do print(self.a+self.L*randfun())
.
Why is that lambda function without arguments essential to this piece of code? What does it do?