[Python-Dev] Extended Function syntax

Duncan Booth duncan@rcp.co.uk
Fri, 24 Jan 2003 13:22:04 +0000


"Martin v. Löwis" <martin@v.loewis.de> wrote in 
news:3E312900.90100@v.loewis.de:

> Duncan Booth wrote:
>> 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
> 
> This is beautiful, but it does not work: when defining the getter, you 
> need both the old property, and the new function object.

Gah!, I must be asleep today.

Something like this might work (although it is getting a bit messy):

   def set(fn):
       get, delete, doc = None, None, fn.__doc__ 
       namespace = inspect.getcurrentframe().f_back.f_locals
       oldfn = namespace.get(fn.__name__)

       if isinstance(oldfn, property):
           get, delete, doc = oldfn.fget, oldfn.fdel, oldfn.__doc__
       return property(get, fn, delete, doc)

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