[Python-3000] symbols?

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 11 10:18:19 CEST 2006


Kendall Clark wrote:

> Well, some differences. In Ruby, for example, symbols are global in  
> scope and strings aren't.

In Python, interning is global in scope, so intern('foo')
done in one module gives the same object as intern('foo')
done in another module. And since CPython automatically
interns any string literal that looks like an identifier,
using the literal 'foo' gives you exactly the same benefits
as a symbol would, as far as I can see.

The only difference is a syntactic one -- whether you
write it as 'foo' or something else like :foo.

> The other 
> difference (which you may not think essential) is that  strings have 
> lots of methods that are *never* relevant to the uses  one would put a 
> symbol.

I don't see this as a problem. Strings have a lot of
methods that I never use on the vast majority of strings
in my programs, and I don't lose any sleep over it.

>> A symbol type
>> wouldn't solve that problem; enumerations would.
> 
> Nope, :foo and :Foo are different symbols, no doubt about that.

You miss the point. If you type :Foo when you meant :foo,
no error occurs at that point. But if you are using a value
bound to the name foo, referring to it as Foo gives an
immediate NameError.

That's the benefit that enumerations have which symbols
don't -- because you have to explicitly define them, you
get an extra degree of error checking.

--
Greg


More information about the Python-3000 mailing list