[Python-Dev] Extended Function syntax

Shane Holloway (IEEE) shane.holloway@ieee.org
Tue, 28 Jan 2003 14:56:43 -0700


Seems to me like the following should work for this in 2.2 and beyond::

     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     #~ In some common file...

     class Property(object):
         def __init__(prop, __doc__ = ''):
             prop.__doc__ = __doc__
         def __get__(prop, self, klass):
             if self is None:
                 return klass
             else:
                 return prop.Get(self)
         def __set__(prop, self, value):
             prop.Set(self, value)
         def __delete__(prop, self):
             prop.Del(self)

     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     #~ To use...

     class Parrot(object):
         class count(Property):
             def Get(prop, self):
                 return self._count
             def Set(prop, self, value):
                 self._count = value
             def Del(prop, self):
                 self._count = 0
         count = count('Current parrot count')
         _count = 0

As for me, I like the simplistic syntax of method assignments.  What I 
do not like are the polymorphism implications.  For instance::

     class A(object):
         def method(self):
             return "A.method"
         value = property(method)
         alias = method

     class B(A):
         def method(self):
             return "B.method"

     obj = B()
     obj.method() # "B.method"
     obj.value # "A.method"
     obj.alias() # "A.method"

Note that "obj.alias()" is not the same as "obj.method()", but rather is 
equivalent to calling "A.method(obj)".  So, my question is, now that 
I've defined some property methods, how do I override them in a simple 
and straightforward manner?  The crux seems to be the awkwardness of 
properties and aliases, and their relationships to methods.  Well, at 
least to me, it is.  ;)

-Shane Holloway