Last active
July 13, 2021 19:50
-
-
Save Morgul/10805927 to your computer and use it in GitHub Desktop.
Atoms/Symbols in Python
This file contains hidden or 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
class atom(object): | |
"""An implementation of the atom concept, inspired by Erlang. | |
Modified from here: http://www.me.net.nz/blog/atoms-slash-symbols-in-python/ | |
""" | |
def __init__(self, a): | |
self._a = intern(a) | |
def __eq__(self, other): | |
if isinstance(other, atom): | |
return other._a == self._a | |
else: | |
return other == self._a | |
def __ne__(self, another_atom): | |
return not self.__eq__(another_atom) | |
def __repr__(self): | |
return '<atom ' + self._a + '>' | |
def __hash__(self): | |
return hash(self._a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An intriguing idea here, would be to turn this into a module that leveraged python's tokenizer so that
:foo
becomesatom('foo')
. That would mean we could do this:That would be amazing. But, I'm not sure how you'd hook it all up.