Possible PEP: Improve classmethod/staticmethod syntax

Carl Banks imbosol at aerojockey.com
Wed Jun 4 19:17:59 EDT 2003


"Sean Ross" <frobozz_electric at hotmail.com> wrote in message news:<bbl189$fs9$1 at driftwood.ccs.carleton.ca>...
> "Gerrit Holl" <gerrit at nl.linux.org> wrote in message
> news:mailman.1054718112.3943.python-list at python.org...
> >
> > I don't see how properties would fit into any of these syntaxes
> > though, because the property() function takes multiple arguments.
> >
> 
> 
> Perhaps you would have something like the following:
> 
> def foo(self) [property]:
>     "here's the properties doc string"
>     def get():
>         return self.__foo
>     def set(value):
>         self.__foo = value
>     def del():
>         del self.__foo
> 
> Where the property attribute would signal the interpreter to look for nested
> functions 'get', 'set', and possibly 'del'


If you do this:

    def defineproperty(name,bases,clsdict):
        getter = clsdict.get('get',None)
        setter = clsdict.get('set',None)
        deller = clsdict.get('del',None)
        docstr = clsdict.get('__doc__',None)
        return property(getter,setter,deller,docstr)


Then you can make a property metaclass, like this:

    class foo:
        "here's the properties doc string"
        __metaclass__ = defineproperty
        def get(self):
            return self.__foo
        def set(self,value):
            self.__foo = value
        def del(self):
            del self.__foo


I've never used properties, but if I ever do, this is how I'm doing
it.  In fact, I would probably do this with static and class
functions, too.  (I would take the time to make a base class, though,
so I wouldn't have to set __metaclass__ explicitly.)  I think it
should be the STANDARD Python way to define properties and other
special descriptors.


-- 
CARL BANKS




More information about the Python-list mailing list