[Python-Dev] Extended Function syntax

Duncan Booth duncan@rcp.co.uk
Fri, 24 Jan 2003 11:42:55 +0000


John Williams <jrw@pobox.com> wrote in news:3E300A13.6020303@pobox.com:

> Compared to the other proposal going around (which I'll call Guido's, 
> since he brought it up), the really big advantage of my proposal is that 
> you can use it to do something like adding a property to a class 
> implicitly by defining its getter and setter methods:
> 
>    class A(object):
> 
>      def get foo(self):
>        "Getter for property 'foo'."
>        return self.__foo
> 
>      def set foo(self, foo):
>        "Setter for property 'foo'."
>        self.__foo = foo
> 
<snip>
> At this stage I'd much rather see Guido's proposal implemented, unless 
> someone comes up with a truly ingenious way to combine the advantages of 
> both.

How about this:

    class A(object):
 
      def foo(self, foo) [property.set]:
        "Setter for property 'foo'."
        self.__foo = foo

      def foo(self) [property.get]:
        "Getter for property 'foo'."
        return self.__foo

Then add static methods to property that look something like this:

   def set(fn):
       if isinstance(fn, property):
           return property(fn.fget, fn, fn.fdel, fn.__doc__)
       else:
           return property(fset=fn)

   def get(fn): ...
   def delete(fn): ...

-- 
Duncan Booth                                             duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?