<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><br>Good day.<br>You should try <span style="font-weight: bold;">self.count += 1</span> instead of Foo.count += 1.<br><br>--- On <b>Tue, 6/2/09, Juha Salo <i>&lt;jusa.sj@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Juha Salo &lt;jusa.sj@gmail.com&gt;<br>Subject: [Cython] Syntax for declaring Cython class variables<br>To: cython-dev@codespeak.net<br>Date: Tuesday, June 2, 2009, 3:43 AM<br><br><div id="yiv100632139">Hi,&nbsp;<div><br></div><div>I got a simple problem regarding so called class variables in Cython. I'm trying to declare a simple counter as a class variable which is shared between all instances of that class. The counter's value should be incremented as instances of the class are created. Cython allows me to declare a Python dictionary as a class variable,
 but the following gives me an error:</div>
<div><br></div><div><br></div><div># foo.pyx</div><div><br></div><div><div>cdef class Foo:</div><div>&nbsp;&nbsp; &nbsp;count = 0</div><div><br></div><div>&nbsp;&nbsp; &nbsp;def __cinit__(self):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Foo.count += 1</div><div><br></div><div>
<br></div><div><div>&gt;&gt;&gt; a = Foo()</div><div>Traceback (most recent call last):</div><div>&nbsp;&nbsp;File "&lt;stdin&gt;", line 1, in &lt;module&gt;</div><div>&nbsp;&nbsp;File "foo.pyx", line 7, in foo.Foo.__cinit__ (foo.c:388)</div>
<div>&nbsp;&nbsp; &nbsp;Foo.count += 1</div><div>TypeError: can't set attributes of built-in/extension type 'foo.Foo'</div><div><br></div><div><br></div><div>Is there a way to declare an integer class variable in Cython?</div>
</div></div>
</div><br>-----Inline Attachment Follows-----<br><br><div class="plainMail">_______________________________________________<br>Cython-dev mailing list<br><a ymailto="mailto:Cython-dev@codespeak.net" href="/mc/compose?to=Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br><a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br></div></blockquote></td></tr></table><br>