Is a "real" C-Python possible?

Kay Schluehr kay.schluehr at gmx.net
Wed Dec 12 16:43:30 EST 2007


On Dec 12, 8: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? This is almost identical syntax (but
> with less indentation) to a C# property declaration.

C# properties are thunk statements:

private Object _foo = null;

public Object foo {
  get { return this._foo; }
  set { this._foo = value; }
}

In Python pseudo code this would translate to

foo:
    def get(self): return self._foo
    def set(self, value): self._foo = value

omitting the reference to a property constructor. This is the pure
essence: assign methods not to objects but object attributes, for
which certain protocols are defined. It could be generalized for GUI
applications using triggers or other dataflow related bindings. The
"pythonic" solution being mentioned is a rather obscure and convoluted
decorator hack. "Executable pseudocode" reads different and for the
latter assertion one doesn't need a BDFL stamped license. It
demonstrates the cleverness of the programmer more than it clarifies
the issue.

Kay



More information about the Python-list mailing list