Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Created November 30, 2021 19:55
Show Gist options
  • Save Glutexo/04584e98cb0db615b13ced3c7bc4cdfb to your computer and use it in GitHub Desktop.
Save Glutexo/04584e98cb0db615b13ced3c7bc4cdfb to your computer and use it in GitHub Desktop.
A mutable cell with the simplest API possible
_SENTINEL = object()
class Cell:
def __init__(self, value=_SENTINEL):
if value is not _SENTINEL:
self.value = value
def __call__(self, value=_SENTINEL):
if value is not _SENTINEL:
self.value = value
if not hasattr(self, "value"):
raise ValueError("Cell is empty")
return self.value
if __name__ == "__main__":
cell = Cell()
# cell()
cell("x")
print(cell())
cel = Cell("y")
print(cel())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment