Skip to content

Instantly share code, notes, and snippets.

@Morgul
Last active July 13, 2021 19:50
Show Gist options
  • Save Morgul/10805927 to your computer and use it in GitHub Desktop.
Save Morgul/10805927 to your computer and use it in GitHub Desktop.
Atoms/Symbols in Python
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)
@Morgul
Copy link
Author

Morgul commented Apr 16, 2014

An intriguing idea here, would be to turn this into a module that leveraged python's tokenizer so that :foo becomes atom('foo'). That would mean we could do this:

>>> :foo
<atom foo>
>>> a'foo'
<atom foo>
>>> a"foo"
<atom foo>

That would be amazing. But, I'm not sure how you'd hook it all up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment