Is a "real" C-Python possible?
George Sakkis
george.sakkis at gmail.com
Wed Dec 12 15:57:04 EST 2007
On Dec 12, 2:23 pm, "Chris Mellon" <arka... at gmail.com> wrote:
> On Dec 12, 2007 12:53 PM, George Sakkis <george.sak... at gmail.com> wrote:
>
>
>
> > 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.
>
> How exactly is this a kludge?
In three (at least) ways:
1. The property name ('foo') is repeated *5* times for a single class.
Talk about DRY.
2. Total inconsistency: @property for the getter when it is defined
for the first time, @foo.setter/@foo.deletter for the setter/deletter,
@foo.getter when the getter is redefined. WTF ?!
> This is almost identical syntax (but with less indentation) to a C# property declaration.
3. Less indentation is not an advantage here; typically ones wants all
two or three related functions that define the property to stand out
as a separate group, not be mixed with regular public methods.
Sorry, C# wins hands down on this.
George
More information about the Python-list
mailing list