Python-2.6 extended the definition of property to include getter,
setter, and deleter methods that can be used as decorators. This
allows a really nice pattern for declaring properties without
populating the namespace with a bunch of private methods:
class Foo(object):
_bar = None
@property
def bar(self):
return self._bar
@bar.setter
def bar(self, val):
self._bar = val
What happens is @bar.setter makes a copy of bar, updates the copy to
use the decorated method as the setter, and returns the copy, which is
then bound to the name of the decorated function. The decorated
function has to be called bar, so that the updated copy is bound to
bar, replacing the original.
Attempting to cythonize this example yields an error:
$ python setup.py build_ext --inplace
running build_ext
cythoning test_property.pyx to test_property.c
Error converting Pyrex file to C:
------------------------------------------------------------
...
_bar = None
@property
def bar(self):
return self._bar
@bar.setter
def bar(self, val):
^
------------------------------------------------------------
/Users/darren/temp/test/test_property.pyx:11:4: 'bar' already declared
building 'test_property' extension
/usr/bin/gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -O2
-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
-I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
-c test_property.c -o
build/temp.macosx-10.6-x86_64-2.7/test_property.o
test_property.c:1:2: error: #error Do not use this file, it is the
result of a failed Cython compilation.
error: command '/usr/bin/gcc-4.2' failed with exit status 1
Would it be possible for Cython to support this pattern? I think it
will be increasingly common. For example: I recently pointed it out on
the PyQt and PySide mailing lists. In both cases, the developers were
pretty enthusiastic about the pattern once they were aware of it. The
most recent versions of PyQt and dip now support it for their own
properties, and the PySide developers are working on implementing it.
Thanks,
Darren
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev