[Python-ideas] C# style properties

Neil Toronto ntoronto at cs.byu.edu
Fri Jul 13 11:43:07 CEST 2007


Josiah Carlson wrote:
> Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>   
>> Josiah Carlson wrote:
>>     
>>>     class foo:
>>>         class a(Property):
>>>             ''' documentation '''
>>>             def get(self):
>>>                 return self._a
>>>             ...
>>>
>>> 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.
>>>       
>> I would dispute that anything involving a class statement
>> that creates something other than a class is "obvious".
>>     
>
> The "Property" statement that is longer, the existance of get/set/de1
> methods, those didn't catch your eye?  What if the user plopped in a
> nice big "__metaclass__ = Property" line?  Ugly or not, I find that it
> has helped me organize (with indentation), not repeat myself, and not
> have to clean up spare get/set/del methods.  I personally prefer it to
> x = property(...), a property decorator, and a bazillion times more than
> any property-specific syntax.
>   

Your original question was on how obvious this is to *invent*, but here 
you're defending how obvious it is to *read*. (FWIW, I think it reads 
just fine, and I've added a couple of new, nifty things to my personal 
library. Thanks for the pointer.) Inner-class-as-a-class-variable is the 
mental leap you have to make, plus you have to be comfortable with 
metaclasses. I expect most developers would get to "hey, let's add a new 
keyword" before they arrive at that. Some proportion of those would 
bring it up on a mailing list, and then some proportion of the mailing 
list recipients would have seen it before, and some proportion of those 
recipients might feel a little exasperated and wonder how the rest of us 
can be so blind as to miss such an obvious solution...

It's statistics, babay.

On a very related topic, I find it interesting how often classes get 
abused just to provide a convenient namespace to stick things in. 
Classes are heavy things, though, with a lot of semantics and 
behind-the-scenes witchery to deal with inheritance and metaclasses and 
such-like. Why bring that baggage into it? It might be nice to be able 
to declare a one-off namespace:

    class Foo:
        x:
            '''documentation'''
            def __get__(self, instance, owner):  # self = x, instance = 
a Foo(), owner = Foo()
                return instance._x


    logger:
        '''Singleton logger object blah blah...'''
        db = make_db_connection(...)
       
        def log(self, message):
            ...


Be gentle, because I haven't thought this through very well. :)

Neil




More information about the Python-ideas mailing list