From b53fd52f471cf079c39b347576cd3999abcc1bf5 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 21 Apr 2025 14:01:16 +0000 Subject: [PATCH] util: new `cached_property` decorator --- mmgen/util.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mmgen/util.py b/mmgen/util.py index 0c9f7100..0097c45a 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -470,3 +470,12 @@ def have_sudo(*, silent=False): return True except: return False + +def cached_property(orig_func): + @property + def new_func(self): + attr_name = '_' + orig_func.__name__ + if not hasattr(self, attr_name): + setattr(self, attr_name, orig_func(self)) + return getattr(self, attr_name) + return new_func