On Tue, Jun 2, 2009 at 2:48 PM, Juha Salo <[email protected]> wrote:
> OK, thanks for the info. It's interesting, though, that I can code something
> like this in Cython and it works:
>
> cdef class Foo:
>     foos = {}
>     def __cinit__(self, name):
>         Foo.foos[name] = self
>
>>>> a = Foo('Larry')
>>>> b = Foo('Bob')
>>>> a.foos
> {'Bob': <foo.Foo object at 0x01C5F490>, 'Larry': <foo.Foo object at
> 0x01C5F488>}
>>>> b.foos
> {'Bob': <foo.Foo object at 0x01C5F490>, 'Larry': <foo.Foo object at
> 0x01C5F488>}
>

But that is a different beast. You are doing a 'getattr', and next
updating the attribute. What you cannot do on a cdef class is
'setattr'. Python rules for extension types...

BTW, such code creates reference cycles. If you are going to create a
lot of these objects, GC collection will have to traverse a large
dict, slowing down your runs each time the collection triggers.

> Perhaps I could use a hack workaround with the aforementioned count variable
> by storing it inside a class level list object.
>

Well, if the extra dict lookup is not an issue for you, go for it....



> 2009/6/2 Chris Colbert <[email protected]>
>>
>> This is correct, forget what I said. I should have tried it out before I
>> said anything. My apologies.
>>
>>
>>
>> On Tue, Jun 2, 2009 at 12:44 PM, Lisandro Dalcin <[email protected]>
>> wrote:
>>>
>>> No, that will not work. AFAIK, Class attributes are not currently
>>> supported for cdef classes.
>>>
>>> Juha, you will have to code like this:
>>>
>>>
>>> # foo.pyx
>>>
>>> cdef int Foo_count = 0
>>>
>>> cdef class Foo:
>>>
>>>    def __cinit__(self):
>>>        global Foo_count
>>>        Foo_count += 1
>>>
>>>
>>>
>>>
>>> On Tue, Jun 2, 2009 at 1:36 PM, Chris Colbert <[email protected]>
>>> wrote:
>>> > you have to declare the attribute as public.
>>> >
>>> > http://docs.cython.org/docs/extension_types.html
>>> >
>>> > Cheers,
>>> >
>>> > Chris
>>> >
>>> > On Tue, Jun 2, 2009 at 11:17 AM, Juha Salo <[email protected]> wrote:
>>> >>
>>> >> Unfortunately that didn't seem to work. I got the following error
>>> >> after
>>> >> the change:
>>> >> >>> a = Foo()
>>> >> Traceback (most recent call last):
>>> >>   File "<stdin>", line 1, in <module>
>>> >>   File "foo.pyx", line 7, in foo.Foo.__cinit__ (foo.c:388)
>>> >>     self.count += 1
>>> >> AttributeError: 'foo.Foo' object attribute 'count' is read-only
>>> >>
>>> >> To clarify, I want the value of the count variable to remain the same
>>> >> between all instances. So if I create 3 Foo objects, each object
>>> >> should show
>>> >> 3 as the value in the count variable.
>>> >>
>>> >> 2009/6/2 Cristi Constantin <[email protected]>
>>> >>>
>>> >>> Good day.
>>> >>> You should try self.count += 1 instead of Foo.count += 1.
>>> >>
>>> >>
>>> >> _______________________________________________
>>> >> Cython-dev mailing list
>>> >> [email protected]
>>> >> http://codespeak.net/mailman/listinfo/cython-dev
>>> >>
>>> >
>>> >
>>> > _______________________________________________
>>> > Cython-dev mailing list
>>> > [email protected]
>>> > http://codespeak.net/mailman/listinfo/cython-dev
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Lisandro Dalcín
>>> ---------------
>>> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
>>> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
>>> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
>>> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
>>> Tel/Fax: +54-(0)342-451.1594
>>> _______________________________________________
>>> Cython-dev mailing list
>>> [email protected]
>>> http://codespeak.net/mailman/listinfo/cython-dev
>>
>>
>> _______________________________________________
>> Cython-dev mailing list
>> [email protected]
>> http://codespeak.net/mailman/listinfo/cython-dev
>>
>
>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to