Re: Proposal for adding symbols within Python
Pierre Barbier de Reuille wrote:[color=blue]
> 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.[/color]
How about using the prefix "symbol." instead of "$"?
[color=blue][color=green][color=darkred]
>>> symbol.x[/color][/color][/color]
symbol.x[color=blue][color=green][color=darkred]
>>> symbol.y[/color][/color][/color]
symbol.y[color=blue][color=green][color=darkred]
>>> x = symbol.x
>>> x == symbol.x[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x == symbol.y[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> symbol.file.ope ned[/color][/color][/color]
symbol.file.ope ned[color=blue][color=green][color=darkred]
>>> symbol.file.clo sed[/color][/color][/color]
symbol.file.clo sed[color=blue][color=green][color=darkred]
>>> symbol.spam(sym bol.eggs)[/color][/color][/color]
symbol.spam(sym bol.eggs)
And the definition of symbol that I used:
[color=blue][color=green][color=darkred]
>>> class symbol(object):[/color][/color][/color]
.... class __metaclass__(t ype):
.... def __getattr__(cls , name):
.... return symbol(name)
.... def __getattr__(sel f, 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).__n ame__, 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
Pierre Barbier de Reuille wrote:[color=blue]
> 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.[/color]
How about using the prefix "symbol." instead of "$"?
[color=blue][color=green][color=darkred]
>>> symbol.x[/color][/color][/color]
symbol.x[color=blue][color=green][color=darkred]
>>> symbol.y[/color][/color][/color]
symbol.y[color=blue][color=green][color=darkred]
>>> x = symbol.x
>>> x == symbol.x[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x == symbol.y[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> symbol.file.ope ned[/color][/color][/color]
symbol.file.ope ned[color=blue][color=green][color=darkred]
>>> symbol.file.clo sed[/color][/color][/color]
symbol.file.clo sed[color=blue][color=green][color=darkred]
>>> symbol.spam(sym bol.eggs)[/color][/color][/color]
symbol.spam(sym bol.eggs)
And the definition of symbol that I used:
[color=blue][color=green][color=darkred]
>>> class symbol(object):[/color][/color][/color]
.... class __metaclass__(t ype):
.... def __getattr__(cls , name):
.... return symbol(name)
.... def __getattr__(sel f, 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).__n ame__, 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
Comment