Last active
November 30, 2021 20:41
-
-
Save Glutexo/7480936b2d0b58b253d1e8d55c3db46b to your computer and use it in GitHub Desktop.
A cell object that doesn’t require knowledge of any names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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