Proposal for adding symbols within Python

Steven Bethard steven.bethard at gmail.com
Wed Nov 16 19:41:25 EST 2005


Pierre Barbier de Reuille wrote:
> Proposal
> ========
> 
> First, I think it would be best to have a syntax to represent symbols.
> Adding some special char before the name is probably a good way to
> achieve that : $open, $close, ... are $ymbols.

How about using the prefix "symbol." instead of "$"?

 >>> symbol.x
symbol.x
 >>> symbol.y
symbol.y
 >>> x = symbol.x
 >>> x == symbol.x
True
 >>> x == symbol.y
False
 >>> symbol.file.opened
symbol.file.opened
 >>> symbol.file.closed
symbol.file.closed
 >>> symbol.spam(symbol.eggs)
symbol.spam(symbol.eggs)

And the definition of symbol that I used:

 >>> class symbol(object):
...     class __metaclass__(type):
...         def __getattr__(cls, name):
...             return symbol(name)
...     def __getattr__(self, name):
...         return symbol('%s.%s' % (self.name, name))
...     def __init__(self, name):
...         self.name = name
...     def __eq__(self, other):
...         return self.name == other.name
...     def __repr__(self):
...         return '%s.%s' % (type(self).__name__, self.name)
...     def __call__(self, *args):
...         arg_str = ', '.join(str(arg) for arg in args)
...         return symbol('%s(%s)' % (self.name, arg_str))
...

It doesn't work with "is", but otherwise I think it's pretty close to 
your proposal, syntax-wise.  Is there something obvious this won't 
address for you?

STeVe



More information about the Python-list mailing list