why no ++?

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Aug 23 21:17:12 EDT 2001


Bengt Richter wrote:
> 
> I too was thinking that a symbol object might be a useful thing.
> Say it was created with the expression
>     sym = .

(Warning: untested code follows)

# Module Symbols

_symbols = {}

class Symbol:

  def __init__(self, name):
    self.name = name

  def __repr__(self):
    return "<symbol '%s'>" % self.name

class _SymbolMaker:

  def __getattr__(self, name):
    sym = _symbols.get(name, None)
    if not sym:
      sym = Symbol(name)
      _symbols[name] = sym
    return sym

symbol = _SymbolMaker()

# Example

from Symbols import symbol

my_uncle = symbol.bob
my_aunt = symbol.mary
if my_uncle is symbol.bob:
  print "Bob's my uncle"

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list