I suggest using a cached property
class HoldAttr(object):
@cached_property
def foundat(self):
return []
@property
def was_found(self):
return "foundat" in self.__dict__
if __name__ == "__main__":
h = HoldAttr()
for i in range(3):
h.foundat.append(i)
print(h.foundat)
print(h.was_found)
h = HoldAttr()
print(h.was_found)
""" my output -->
[0, 1, 2]
True
False
"""
However, a more classical style makes your code more readable
class HoldAttr(object):
foundat = None
def add_found(self, at):
if self.foundat is None:
self.foundat = [at]
else:
self.foundat.append(at)
def was_found(self):
return self.foundat is not None
if __name__ == "__main__":
h = HoldAttr()
for i in range(3):
h.add_found(i)
print(h.foundat)
print(h.was_found())
h = HoldAttr()
print(h.was_found())
Code with straightforward behavior is better.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11