c# - Relevance of CodeContext when calling IronPython function -
i'm calling ironpython function c#. seems on definition, function captures original scope. when later phone call without explicit scope, can still access values original scope. if alter scope values, correctly reads new values. take @ example:
using ironpython.hosting; using ironpython.runtime; using ironpython.runtime.operations; using microsoft.scripting; using microsoft.scripting.hosting; namespace ironpythonfunctest { class programme { static void main() { scriptengine scriptengine = python.createengine(); // create scope global value script utilize scriptscope scriptscope = scriptengine.createscope(); scriptscope.setvariable("someglobalvalue", 10); // execute script defining function foo(x) string script = "def foo(): print(someglobalvalue)"; scriptsource scriptsource = scriptengine. createscriptsourcefromstring(script, sourcecodekind.statements); scriptsource.execute(scriptscope); // extract foo scope pythonfunction foo = scriptscope.getvariable<pythonfunction>("foo"); // alter someglobalvalue scriptscope.setvariable("someglobalvalue", 42); // phone call foo. prints 42, not 10 (or nothing). pythoncalls.call(foo); } } } now i'm wondering: overloads of pythoncalls.call() expect codecontext object (which, if understand correctly, represents scope). losing if phone call ironpython function above, without passing code context? given function apparently captured original scope on creation, there doesn't seem point in passing additional code context. there situations makes difference whether do?
why phone call foo on pythoncall.call? seek this: scriptscope.host.scriptengine.operations.invokemember(class-instance, method-name, arguments); ironpython correctly handle codecontext on it's own. can found sample implementation here: dlrclass/invokemember.
as far know, codecontext more scope, take @ definition:
/// <summary> /// captures , flows state of executing code generated /// python code ironpython runtime. /// </summary> [debuggertypeproxy(typeof(codecontext.debugproxy)), debuggerdisplay("module: {modulename}", type="module")] public sealed class codecontext { // ... } hope helps!
c# ironpython
No comments:
Post a Comment