On Fri, Mar 18, 2011 at 8:58 PM, Jim Jewett <span dir="ltr"><<a href="mailto:jimjjewett@gmail.com">jimjjewett@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Fri, Mar 18, 2011 at 9:37 PM, Greg Ewing <<a href="mailto:greg.ewing@canterbury.ac.nz">greg.ewing@canterbury.ac.nz</a>> wrote:<br>
> Guido van Rossum wrote:<br>
<br>
>> There's another pattern where all class attributes that have a certain<br>
>> property are also collected in a per-class datastructure,<br>
<br>
> I think __addtoclass__ could cover those as well,<br>
> if you can arrange for the relevant objects to inherit<br>
> from a class having an appropriate __addtoclass__<br>
> implementation.<br>
<br>
</div>How do you put an attribute (such as __addtoclass__ ) on a name?  Or<br>
are you proposing that the actual pattern be something more like:<br>
<br>
     x=SpecialObj()<br>
<br>
And that normal initiation be handled either later, or as part of the<br>
SpecialObj initiation<br>
<br>
    x=SpecialObj()=5<br>
or<br>
    x=SpecialObj(); x=5<br>
or<br>
    x=SpecialObj(value=5)<br></blockquote><div><br>What we're describing only applies to class variables; a top-level variable wouldn't be affected.<br><br>Imagine for instance a column class (ORMish) that wants to know its name:<br>

<br>class Column(object):<br>    def __init__(self, **kw):<br>        ...<br>    def __addtoclass__(self, name, cls):<br>        <a href="http://self.name">self.name</a> = name<br>        return self<br><br>Now if you do:<br>

<br>class MyTable:<br>    username = Column()<br><br>Then <a href="http://MyTable.username.name">MyTable.username.name</a> == 'username'<br><br>If you wanted to be able to reuse values, like Greg wants, you could do:<br>

<br>class Column(object):<br>    def __init__(self, kw):<br>        <a href="http://self.kw">self.kw</a> = kw<br>        ...<br>    def copy(self):<br>        return self.__class__(<a href="http://self.kw">self.kw</a>)<br>

    def __addtoclass__(self, name, cls):<br>        new_obj = self.copy()<br>        <a href="http://new_obj.name">new_obj.name</a> = name<br>        return new_obj<br><br>Or you could use several different classes (e.g., BoundColumn), or... well, there's many ways to skin a cat.<br>

<br>Like descriptors, only objects that implement this new method would participate.  It's not really like a decorator, it's much more like descriptors -- decorators like classmethod just happen to be descriptor factories.<br>

<br>  Ian<br><br></div></div>