[Python-ideas] C# style properties

Josiah Carlson jcarlson at uci.edu
Thu Jul 12 18:51:15 CEST 2007


Christian Heimes <lists at cheimes.de> wrote:
> 
> Hello!
> 
> In the past few months I've been toying around with .NET, C# and
> PythonNet. While I still think that C# is too wory (public static
> explicit operator Egg(Spam spam) { ... }) C# has one syntax feature I
> really like to see in Python.
> 
> private float _a
> public float a
> {
>     get { return _a; }
>     set { _a = value; }
> }
[snip]
> Comments?

Python has an exact equivalent to the C# method (which uses a new
block/scope for defining properties...)

    class foo:
        class a(Property):
            ''' documentation '''
            def get(self):
                return self._a
            def set(self, val):
                self._a = val
            def de1(self):
                del self._a


Now all you need is a Property base class with a proper metaclass that
handles all of the magic for you.  I'm curious as to why this still gets
brought up when the obvious syntax is more or less identical to
basically all reasonable alternate syntaxes.  Is it because people don't
understand that metaclasses can mangle your class any way they want?


 - Josiah




More information about the Python-ideas mailing list