[Python-Dev] Definining properties - a use case for class decorators?

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

<callable> <name> <tuple>:

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):
    def fset(self):
    def fdel(self):

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():
  def do_that():
  def default():


Notice that the proposal is already implementable by abusing the class

class <name> <tuple>:
   __metaclass__ = <callable>

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


type <classname> <bases>:


               Michele Simionato

More information about the Python-Dev mailing list