[Pyrex] C Globals

Michael P. Dubner pywebmail at list.ru
Thu Aug 24 08:00:50 UTC 2006


Kent Borg wrote:
> On Wed, Aug 16, 2006 at 01:24:01PM +1200, Greg Ewing wrote:
>   
>> Kent Borg wrote:
>>     
>>> How do I share a global C variable between two Pyrex functions?
>>>       
>> By declaring it as a C variable at the module level,
>> and using a global declaration inside each function
>> which wants to assign to it.
>>
>>    cdef int my_int
>>
>>    def my_func():
>>      global my_int
>>      my_int = 42
>>
>>    def my_other_func():
>>      some_useful_func(my_int)
>>     
>
> Yes, that works!  Thanks.  (I thought the "global my_int" would try to
> create a Python variable, I was trying things like "global cdef int
> my_int".)
>
> Another question: Why doesn't my_other_func() not need the global
> declaration too?  At first I thought you made a mistake, but it works
> as you wrote it.  It also appears to work with a second global
> declaration in my_other_func.
>
> Only the first guy who uses it needs the declaration?  Is there any
> harm in putting the global declaration everywhere that uses it to make
> it clearer what is going on?
>   
It's really Python question.
Answer is - you need declare variable global in function where you want
to assign to it, not where you reading it.

--
Best regards,
Michael Dubner (dubnerm.at.mindless.com)
Brainbench MVP/HTML+JavaScript (http://www.brainbench.com)




More information about the Pyrex mailing list