Q:

Re-evaluating a var in python

Consider the following pseudo-code:
class Atrribute(object):
   def __init__(self):
       self.value = 0
   def get():
       self.value = random(10)

def func(var):
   lambda var: return var+1

1) myFunc = func(Attribute().get())
2) myFunc()
3) myFunc()

Now, when I call myFunc() in line 2 I get a random value from 0 to 10 (plus one) which was produced by the get() function in Attribute. The problem is that when I call myFunc() in line 3, I get the same random value from line 2. Is there anyway I can make python initiate the get() function again on line 3 to produce a new random value?
Meir

A:

Yes, there is:
import random

class Attribute(object):
   def __init__(self):
       self.value = 0
   def get(self):
       self.value = random.random() * 10
       return self.value

def func(fn):
   return lambda: fn() + 1

att = Attribute()
myFunc = func(att.get)
print myFunc()
print myFunc()

In future, please make sure that any code that you post is free of syntax errors and actually does what you say it does.

