As the Q&A Can modules have properties the same way that objects can?
But I just want to use y
as module.y
, so I run
module.py
import time
def __getattr__(name):
if name == 'y':
return time.time()
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
main.py
import time
from module import y
x = time.time()
print(x)
time.sleep(1)
print(y)
time.sleep(1)
print(y)
But the result of y
won't change, It always equals to x
.
How to solve the problem?
I expect y
always return the current time.