[Pyrex] C Globals

Kent Borg kentborg at borg.org
Wed Aug 16 14:21:30 UTC 2006


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?


Thanks again,

-kb, the Kent who is very pleased he found Pyrex.



More information about the Pyrex mailing list