Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81556
| Date | 2014-11-26 10:22 +0000 |
|---|---|
| From | Kasper Peeters <kasper@phi-sci.com> |
| Subject | PyEval_GetLocals and unreferenced variables |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16315.1416997980.18130.python-list@python.org> (permalink) |
I have a question about PyEval_GetLocals(). The normal behaviour of
PyEval_GetLocals(), and in fact of the locals() function in Python
itself, is to return a list which includes only those variables which
are actually referenced in the local scope. Example:
def fun():
q=3
def fun2():
print(locals())
fun2()
fun()
will print "{}" because "q" has not been referenced in fun2(). On the
other hand, if you do
def fun():
q=3
def fun2():
print(q)
print(locals())
fun2()
fun()
you will instead get "{'q': 3}" as output. All fine and understood.
My question: I want to call a C function inside fun2(), which I want to
give access to the 'q' variable in the fun() scope, _without_ there
being any reference to 'q' in the python code itself. That is, I want
to do:
def fun():
q=3
def fun2():
cfun()
fun2()
fun()
and access 'q' inside the C-function cfun(). If I simply let it call
PyEval_GetLocals, then the result will again not contain "q". Is there
any way in which I can convince python to pull 'q' into the local scope
from within my C code?
Thanks!
Cheers,
Kasper
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
PyEval_GetLocals and unreferenced variables Kasper Peeters <kasper@phi-sci.com> - 2014-11-26 10:22 +0000
csiph-web