[Python-ideas] Keyword/Symbol literals

Steven D'Aprano steve at pearwood.info
Wed Jan 21 12:40:39 CET 2015

On Tue, Jan 20, 2015 at 11:48:58PM -0800, Stephen Hansen wrote:
> >
> > *What do people do now?*
> > or strings
> >
> > df.sort('name')
> ...
> > strings work but feel unpleasant
> Why?
> What does :foo get you that 'foo' doesn't?

It gets you an atomic data type which isn't a string and isn't an int. 

The point of data types is what they *don't allow* as much as what they 
do allow. Symbols is that they are atomic symbolic names, not lists, 
ints, dicts or strings.

'foo'.upper()[1:] is a meaningful operation. 'foo' is a sequence of 
characters, so you can slice it, and characters can be uppercased and 
lowercased and case-folded. Strings have prefixes and suffixes and 

If :foo is merely yet another syntax for creating strings, then it is 
pointless and you should just use a string. But the point of symbols is 
that they don't support string operations. :foo.upper()[1:] is a 
meaningless operation, as is :foo + 23.

Python already has a number of symbol-like objects in the language:

True and False

and possible a few others. With the exception of True and False, most of 
them have virtually no "interesting" or public methods: e.g. you can 
convert None into a string, but you can't do None.upper()[1:]. Although, 
arguably, they are more like Enums than symbols, at least the way Julia 
uses symbols.


I'd like to learn more about Julia-style metaprogramming with symbols. 
The alternative is to treat symbols just as an object with a name and 
little behaviour (like None), which is not as interesting and more 
suited to Enums.


More information about the Python-ideas mailing list