On 10/18/05, Guido van Rossum <[EMAIL PROTECTED]> wrote: > I wonder if at some point in the future Python will have to develop a > macro syntax so that you can write > > Property foo: > def get(self): return self._foo > ...etc...
This reminds me of an idea I have kept in my drawer for a couple of years or so. Here is my proposition: we could have the statement syntax <callable> <name> <tuple>: <definitions> to be syntactic sugar for <name> = <callable>(<name>, <tuple>, <dict-of-definitions>) For instance properties could be defined as follows: def Property(name, args, dic): return property( dic.get('fget'), dic.get('fset'), dic.get('fdel'), dic.get('__doc__')) Property p(): "I am a property" def fget(self): pass def fset(self): pass def fdel(self): pass Another typical use case could be a dispatcher: class Dispatcher(object): def __init__(self, name, args, dic): self.dic = dic def __call__(self, action, *args, **kw): return self.dic.get(action)(*args, **kw) Dispatcher dispatch(action): def do_this(): pass def do_that(): pass def default(): pass dispatch('do_this') Notice that the proposal is already implementable by abusing the class statement: class <name> <tuple>: __metaclass__ = <callable> <definitions> But abusing metaclasses for this task is ugly. BTW, if the proposal was implemented, the 'class' would become redundant and could be replaced by 'type': class <classname> <bases>: <definitions> <=> type <classname> <bases>: <definitions> ;) Michele Simionato _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com