<br><br><div class="gmail_quote">On 4 January 2011 00:56, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org">guido@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">> On 20110103 13:22, Dirkjan Ochtman wrote:<br>
</div><div class="im">>> Do you have actual use cases for these?<br>
<br>
</div><div class="im">On Mon, Jan 3, 2011 at 2:06 PM, K. Richard Pixley <<a href="mailto:rich@noir.com">rich@noir.com</a>> wrote:<br>
> Yes.  Here's a toy example for abstractclassproperty:<br>
<br>
</div>Um, a toy example is pretty much the opposite of a use case. :-(<br>
<br>
That said, I am sure there are use cases for static property and class<br>
property -- I've run into them myself.<br>
<br>
An example use case for class property: in App Engine, we have a Model<br>
class (it's similar to Django's Model class). A model has a "kind"<br>
which is a string (it's the equivalent of an SQL table name). The kind<br>
is usually the class name but sometimes there's a need to override it.<br>
However once the class is defined it should be considered read-only.<br>
Currently our choices are to make this an instance property (but there<br>
are some situations where we don't have an instance, e.g. when<br>
creating a new instance using a class method); or to make it a class<br>
attribute (but this isn't read-only); or to make it a class method<br>
(which requires the user to write M.kind() instead of M.kind). If I<br>
had class properties I'd use one here.<br></blockquote><div><br><br>A class property that can be fetched is very easy to implement. Because of asymmetry in the descriptor protocol I don't think you can create a class property with behaviour on set though (unless you use a metaclass I guess).  <br>
<br>
<span class="pykeyword">class</span> <span class="pytext">classproperty</span><span class="pyoperator">(</span><span class="pytext">object</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br>
   <span class="pykeyword">def</span> <span class="pytext">__init__</span><span class="pyoperator">(</span><span class="pytext">self</span><span class="pyoperator">,</span> <span class="pytext">function</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br>

       <span class="pytext">self</span><span class="pyoperator">.</span><span class="pytext">_function</span> <span class="pyoperator">=</span> <span class="pytext">function</span><br>
   <span class="pykeyword">def</span> <span class="pytext">__get__</span><span class="pyoperator">(</span><span class="pytext">self</span><span class="pyoperator">,</span> <span class="pytext">instance</span><span class="pyoperator">,</span> <span class="pytext">owner</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br>

       <span class="pykeyword">return</span> <span class="pytext">self</span><span class="pyoperator">.</span><span class="pytext">_function</span><span class="pyoperator">(</span><span class="pytext">owner</span><span class="pyoperator">)</span><br>
<br>All the best,<br><br>Michael<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<font color="#888888"><br>
--<br>
--Guido van Rossum (<a href="http://python.org/%7Eguido" target="_blank">python.org/~guido</a>)<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
<br>