C-API: Get scope in function

M

moerchendiser2k3

Hi,

I have a small problem how to get the scope from a C-API function.
Check out this code snippet:

Code:
variable = 3

def test():
    print variable #output: 3
    print globals() # ... 'variable': 3, ...

test()
[/code


In my case I know there is a variable in the scope where this function
is called from, but I dont know how to get it.
Check out:

[code]
PyObject *MyFunction_Test(PyObject* self, PyObject *args, PyObject
*keywords)
{
     ... ?
}

Is it possible to get the dictionary of the scope where this function
is called from?


Thanks a lot!!

Bye, googler
 
G

Gabriel Genellina

En Sun, 17 Jan 2010 10:23:03 -0300, moerchendiser2k3
I have a small problem how to get the scope from a C-API function.
Check out this code snippet:

Code:
variable = 3

def test():
print variable #output: 3
print globals() # ... 'variable': 3, ...

test()
[/code


In my case I know there is a variable in the scope where this function
is called from, but I dont know how to get it.[/QUOTE]

Note that in this case the relevant scope is the one where the Python  
function was *defined*, not where it is *called*.
[QUOTE]
Check out:

[code]
PyObject *MyFunction_Test(PyObject* self, PyObject *args, PyObject
*keywords)
{
... ?
}

Is it possible to get the dictionary of the scope where this function
is called from?

Yes, you can obtain the global variables of the current execution frame
using PyEval_GetGlobals()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top