Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Last active November 30, 2021 20:41
Show Gist options
  • Save Glutexo/7480936b2d0b58b253d1e8d55c3db46b to your computer and use it in GitHub Desktop.
Save Glutexo/7480936b2d0b58b253d1e8d55c3db46b to your computer and use it in GitHub Desktop.
A cell object that doesn’t require knowledge of any names
_NOTHING = object()
DELETE = object()
class Cell:
def __init__(self, value=_NOTHING):
if value is not _NOTHING:
self.value = value
def __call__(self, value=_NOTHING):
if value is DELETE:
del self.value
else:
if value is not _NOTHING:
self.value = value
return self.value
def __getattr__(self, _key):
if "value" not in self.__dict__:
raise ValueError("Cell is empty.")
else:
return self.value
def __setattr__(self, key, value):
if key == "value":
self.__dict__[key] = value
else:
self.value = value
def __delattr__(self, key):
if key == "value":
del self.__dict__[key]
else:
del self.value
def __getitem__(self, _key):
return self.value
def __setitem__(self, _key, value):
self.value = value
def __delitem__(self, key):
del self.value
if __name__ == "__main__":
cell = Cell()
cell("x")
# cell(DELETE)
print(cell["z"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment