Is a "real" C-Python possible?

Duncan Booth duncan.booth at invalid.invalid
Thu Dec 13 16:10:43 EST 2007


Christian Heimes <lists at cheimes.de> wrote:

> Python 2.6 and 3.0 have a more Pythonic way for the problem:
> 
> class A(object):
>     @property
>     def foo(self):
>         return self._foo
> 
>     @foo.setter
>     def foo(self, value)
>         self._foo = value
> 
>     @foo.deletter
>     def foo(self)
>         del self._foo
> 
> class B(A):
>     # one can even overwrite the getter in a subclass
>     @foo.getter
>     def foo(self):
>         return self._foo * 2
> 

That would be great if it worked, but it doesn't.

Fixing your typos (missing colons, spelling of deleter, and in B the 
decorator needs to refer to A.foo.getter):

Python 3.0a2 (r30a2:59405M, Dec  7 2007, 15:23:28) [MSC v.1500 32 bit 
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 3.0a1
>>> class A(object):
    @property
    def foo(self):
        return self._foo

    @foo.setter
    def foo(self, value):
        self._foo = value

    @foo.deleter
    def foo(self):
        del self._foo

        
>>> class B(A):
    # one can even overwrite the getter in a subclass
    @A.foo.getter
    def foo(self):
        return self._foo * 2

>>> a = A()
>>> a.foo = 5
>>> a.foo
10
>>> A.__dict__['foo']
<property object at 0x01261F80>
>>> B.__dict__['foo']
<property object at 0x01261F80>

Unfortunately as currently implemented, getter setter and deleter just 
update the existing property, so the getter defined in B changes how the 
property works in A as well. I think the intention may have been that they 
should create a new property each time, but this isn't what has been 
implemented.



More information about the Python-list mailing list