MarseyWorld/files/helpers/lazy.py

22 lines
435 B
Python
Raw Normal View History

2022-05-25 17:01:29 +00:00
# Prevents certain properties from having to be recomputed each time they are referenced
2022-05-04 23:09:46 +00:00
def lazy(f):
def wrapper(*args, **kwargs):
o = args[0]
2022-05-25 17:01:29 +00:00
if "_lazy" not in o.__dict__:
o.__dict__["_lazy"] = {}
2022-05-04 23:09:46 +00:00
2022-05-25 17:01:29 +00:00
name = f.__name__ + str(args) + str(kwargs),
if name not in o.__dict__["_lazy"]:
o.__dict__["_lazy"][name] = f(*args, **kwargs)
2022-05-04 23:09:46 +00:00
2022-05-25 17:01:29 +00:00
return o.__dict__["_lazy"][name]
2022-05-04 23:09:46 +00:00
wrapper.__name__ = f.__name__
return wrapper