Variable value is determined by function call
This is probably weird but I would like to declare a variable without a fixed value, but "linked" in some way to the result of a function. The goal is for the end user to manipulate a variable, but each time the variable's value is used, its value may change.
This is the current result I get:
from random import randint
def randomfun():
return randint(1, 100)
an_int = randomfun
print an_int # Print the function object
print an_int() # Print the result of randomfun()
What I would like to get is print an_int
to actually call randomfun()
, but without having to add the parenthesis, and the type of an_int
should be randomfun
's return type.
an_int
is an object. It won't change its value unless you change it. However, you could change the way the object is represented:
from random import randint
class RandomFun(object):
def __str__(self):
return str(randomfun())
def randomfun():
return randint(1, 100)
an_int = RandomFun()
print an_int
print an_int
yields (something like)
57
19
链接地址: http://www.djcxy.com/p/75508.html
上一篇: 修改一个Python类
下一篇: 变量值由函数调用确定