why no ++?
Michael Robin
me at mikerobin.com
Wed Aug 22 21:12:52 EDT 2001
The literal string "sym" (becuase it is a valid identifier name) is
already interned, so it is as close to a symbol as you're going to get
in Python. So
"x is y" (or "id(x)==id(y)" ) is alwys true, if x and y are literals
that look the same and have been interned, no matter where they come
from.
Non-literals can be intern()ed explicitly. They are still treated as
any other string. This does most of what you want w/o needing a new
type - if you want different behavior, you could always wrap your
interned string in a class, and define __hash__, __repr__, __str__,
etc.
m
bokr at accessone.com (Bengt Richter) wrote in message news:<3b82bd6d.99311832 at wa.news.verio.net>...
> On 19 Aug 2001 18:28:23 GMT, Bernd.Nawothnig at t-online.de (Bernd Nawothnig) wrote:
> [...]
> >
> >Is there a possibility in Python to return a symbol? In other words: is it
> >possible to write:
> >
> >a() = 5
> >
> >if the function a() returns a symbol like in Lisp:
> >
> >(defun a() 'x)
> >(set (a) 5) ; x is now bound to 5
> >
> [...]
>
> I too was thinking that a symbol object might be a useful thing.
> Say it was created with the expression
> sym = .
> which would work a lot like
> sym='sym'
> except there would be no need for a separate string value. The binding
> could be changed as usual, of course:
> sym = .
> sym = 123
> but
> d = {}
> d[sym]=123
> d['sym']=123
> would make two entries.
> type(sym) would presumably return
> <type 'symbol'>
> and symbol('sym') would return a symbol object, and writing
> sym = symbol('sym')
> would be equivalent to
> sym = .
> It would also be ok to write
> spam = symbol('sym')
>
> Presumably
> str(sym)
> would return
> sym
> not
> 'sym'
> and
> print sym
> would return
> <symbol sym>
> as would
> print spam
> given the assignment preceding above.
>
> You could get a list of symbol objects by, e.g.,
> map(symbol,'abc')
> which would (not currently implemented, obviously) return
> [<symbol a>, <symbol b>, <symbol c>]
>
> It should be legal to have a symbol like this be either key
> or value or both in a dictionary, so
> d={'a':'a', symbol('a'):symbol('a')}
> should be ok.
>
> I would think these kinds of symbols could play a useful role
> as elements of an actual set type.
>
> Hm, should symbol('') be an alias for None ?
>
> If you extended the interpretation of the () trailer when it
> was suffixed to a symbol whose value was a symbol object,
> you could have the effect of your (grabbed and repeated from above)
>
> >(defun a() 'x)
> >(set (a) 5) ; x is now bound to 5
>
> by writing
> a = symbol('x')
> a() = 5
>
> IOW, a=5 would rebind a normally, but a() would access the symbol object
> from symbol('x') and rebind that instead. So () would be like dereferencing
> a sym link.
More information about the Python-list
mailing list