[Python-3000] symbols?

Ian Bicking ianb at colorstudy.com
Tue Apr 11 18:07:38 CEST 2006


Greg Ewing wrote:
> Kendall Clark wrote:
>>One thing I'd really like to see in Python 3000 is support for first- 
>>class symbols,
>>
>>	def web_dispatch("/bank-account", :GET, retrieve_account): pass
> 
> 
> class SymGen:
> 
>    def __getattr__(self, name):
>      return intern(name)
> 
> sym = SymGen()
> 
> web_dispatch("/bank-account", sym.GET, retrieve_account)

The advantage of an enumerator would be more if it was namespace.  E.g.:

   class Sym:
       def __init__(self, parent, name):
           self.parent = parent
           self.name = name
       def __repr__(self):
           return '<Symbol %s.%s>' % (self.parent, self.name)

   class SymGen:
       def __init__(self, name=None):
           if name is None:
               name = self.__class__.__name__
           self.name = name
       def __getattr__(self, attr):
           sym = Sym(self.name, attr)
           setattr(self, attr, sym)
           return sym

Considering the namespace, the difference between enumerations and 
symbols becomes a little more clear (though certainly symbols are used 
for enumerations in languages that have symbols).  You can't use an 
enumeration from a namespace for a method name, for example.

Steven's proposal in another thread for a 'make' statement 
(http://ucsu.colorado.edu/~bethard/py/pep_make_statement.html) would 
alleviate some of the motivation for symbols.  E.g., currently:

   class MyTable(MyORM):
       mycol = Column('mycol', type=Int)

That is, barring metaclass tricks that require cooperation with Column, 
you have to pass the column's name into it.  So 'mycol' is really a 
Python identifier without a namespace, represented as a string.  This is 
what they seem to do a lot of in Ruby.  For instance (assuming class 
decorators were adopted) this is how they might set up a property:

   class Foo:
       @class attr('bar')

Which would add a 'bar' attribute (which doesn't really make sense in 
Python since we have public instance variables, and Ruby only has private).

But the make syntax offers an alternative:

   class MyTable(MyORM):
       make Column mycol:
           type = Int

Now there's no strings, the identifier isn't repeated, and its clear 
that "mycol" is an identifier, not a string or a mere variable binding.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list