<br><div><span class="gmail_quote">On 4/19/07, <b class="gmail_sendername">Jim Jewett</b> &lt;<a href="mailto:jimjjewett@gmail.com">jimjjewett@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 4/18/07, Guido van Rossum &lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt; wrote:<br>&gt; Is anyone available to write up a PEP on how to turn super into a<br>&gt; keyword? Inside regular and class methods, 
super.foo(args) should be<br>&gt; equivalent to super(ThisClass, self).foo(args). I think there are ways<br>&gt; to make the old syntax work too, but maybe that&#39;s only necessary for<br>&gt; 2.6.<br><br>Does this mean it should find the super of the current class (which
<br>isn&#39;t yet defined when the method is being defined)?<br><br>That would be a slight change (though probably an improvement) against<br>today&#39;s lookup-by-name.<br><br>If that change is OK, and no one else volunteers, I&#39;ll try to take a
<br>go at it this weekend.</blockquote><div><br>I&#39;ve had this near the top of my (Python) TODO list for a while, but I haven&#39;t been able to find the time. I&#39;ve considered it while doing the dishes and such, though. I can think of two ways of doing the underlying magic &quot;properly&quot;, and one way that&#39;s too ugly to think about:
<br><br>&nbsp;1) Add a new type of descriptor-hook, for associating an object with the class it is an attribute of. After class creation (in the metaclass __init__), any object with this __associate__ (or whatever) hook gets it called. It&#39;s only called for objects that are attributes of *that* class, not of any superclasses (since they will already be associated with the superclass.) I&#39;m sure there are other uses for this hook, just not methods that want to use super() -- like zope interfaces ;)
<br><br>&nbsp;2) Add a new argument to __get__ (or a new method that acts like __get__ but with the extra argument, and is called instead of __get__ if it is defined) where the extra argument is the class that the retrieved descriptor is actually an attribute of. It currently (correctly) gets the class the attribute was retrieved through, which could be a subclass.
<br><br>In both these cases, the (un)bound method object would end up with knowledge of the class it is defined in, and create a local variable with a super-object for the &#39;super&#39; keyword to use. There still are some grey areas to solve -- what if classes share functions, what if functions share code-objects, etc. #2 is probably a better way to go about it than #1, in that regard.
<br><br></div>(and 3) the fugly method: don&#39;t do anything special with methods, but have super() search through __mro__ for the first class to have the current function as an attribute, then pick the next class from that. Yecch, and more grey areas than the other two methods.)
<br></div><br>There&#39;s also the question of what the super keyword itself should look like, e.g.<br><br>&nbsp; # declaration-style, no dot<br>&nbsp; res = super currentmethod(arg, arg) <br>&nbsp; # treat super like self<br>&nbsp; res = super.currentmethod
(arg, arg)<br>&nbsp; # treat super like self.currentmethod <br clear="all">&nbsp; res = super(arg, arg)<br><br>I think super.currentmethod(arg, arg) makes the most sense, but that may be because it most closely resembles the current practice. However, it may call the &quot;wrong&quot; supermethod when the class does, for instance, &#39;__repr__ = __str__&#39;.
<br>&nbsp;<br>-- <br>Thomas Wouters &lt;<a href="mailto:thomas@python.org">thomas@python.org</a>&gt;<br><br>Hi! I&#39;m a .signature virus! copy me into your .signature file to help me spread!