I would like to have an object that stores an internal function that is evaluate on each read access, so the value returned for those read access is somehow dynamic.
Example :
import time
class MyWrapper:
def __init__(self, function):
self.function = function
# Example
def __read_access__(self):
return self.function()
def test_function():
return (time.time() % 2 ) > 0
test = MyWrapper(test_function)
# Tested multiple times because it's kind of random
print(f"{test=}")
print(f"{test=}")
print(f"{test=}")
# Should be working with 'is' comparator with True, False and None
print(f"{test is True}")
print(f"{test is True}")
print(f"{test is True}")
Example results:
test=True
test=False
test=True
False
False
True
For instance it can be used to bound a variable to the content of a file, or even a database row, in a transparent way for the user of the variable.
The @property
works pretty well but, for what I've understood, it's only for a class method with a instance.value
access, but can't be used directly on a variable.