Is a "real" C-Python possible?

George Sakkis george.sakkis at gmail.com
Wed Dec 12 13:53:29 EST 2007


On Dec 12, 1:12 pm, Christian Heimes <li... at cheimes.de> wrote:

> Kay Schluehr wrote:
> > class A(object):
> >     foo = property:
> >         def fget(self):
> >             return self._foo
> >         def fset(self, value):
> >             self._foo = value
>
> > which was translated as follows:
>
> > class A(object):
> >     def thunk():
> >         def fget(self):
> >             return self._foo
> >         def fset(self, value):
> >             self._foo = value
> >         return vars()
> >     foo = propery(**thunk())
> >     del thunk
>
> 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
>
> Christian

This is by definition Pythonic since it was conceived by the BDFL.It
is also certainly an improvement over the current common practice of
polluting the class namespace with private getters and setters. Still
it's a kludge compared to languages with builtin support for
properties.

George



More information about the Python-list mailing list