[Python-Dev] Definining properties - a use case for class decorators? (original) (raw)

Michele Simionato michele.simionato at gmail.com
Wed Oct 19 10:51:50 CEST 2005


On 10/18/05, Guido van Rossum <guido at python.org> 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

:

to be syntactic sugar for

= (, , )

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 : metaclass =

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 :

<=>

type :

;)

           Michele Simionato


More information about the Python-Dev mailing list