When trying to create a Python function similar to Sage var() or function(), I apparently had a not-so-trivial problem in Python. In fact, a call var('x')in Sage not only returns the symbolic Sage expression, but is equivalent x = SR.var('x'), that is, it assigns the expression object to a variable in the current global namespace (namespace of the calling module).
My question is, how does he do it? If I do something like this:
in B.py:
def func():
globals()['x'] = something
in A.py
from B import func
func()
I can only affect variables in the global namespace B, not the global namespace of the calling module A.
However, the file var.pyxdistributed with my version of Sage is as follows:
...
def var(*args, **kwds):
if len(args)==1:
name = args[0]
else:
name = args
G = globals()
if 'ns' in kwds:
v = SR.var(name, **kwds)
if isinstance(v, tuple):
for x in v:
G[repr(x)] = x
else:
G[repr(v)] = v
return v
...
, Cython . Cython, , , . - Cython, " Python" /CPython?
PS: , , - . .