Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make symbols of a given name always the same symbol.

  • Loading branch information...
commit 24521bb52cecfdecfb6852ad6e24a658eedd77b5 1 parent 9d62e1e
@plathrop authored
Showing with 9 additions and 2 deletions.
  1. +9 −2 pdlisp/symbol.py
View
11 pdlisp/symbol.py
@@ -19,16 +19,23 @@
class Symbol(object):
"""
Simple class to differentiate lisp symbols from the strings that name
- them.
+ them. Each Symbol of a given name is a singleton, calling Symbol(<name>)
+ will always give you the same Symbol object.
"""
+ _symbols = {}
def __init__(self, name):
if valid_symbol_name(name):
self.name = name
else:
raise LispSyntaxError('Invalid symbol name: %s' % name)
-
def __repr__(self):
return "Symbol<%s>" % self.name
+ def __new__(cls, name, *args, **kwargs):
+ if name in cls._symbols:
+ return cls._symbols.get(name)
+ sym = super(Symbol, cls).__new__(cls, name, *args, **kwargs)
+ cls._symbols[name] = sym
+ return sym
def symbol(name):
Please sign in to comment.
Something went wrong with that request. Please try again.