Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add a class to manage the context of a interaction with Python and simplify the variable exchanging #380

Closed
@yagweb

Description

@yagweb

"eval" and "exec" are the most frequently used methods when interacting with Python. However, this two methods implementedhere are hard to use because maintain the global variable dictionary and local variable dictionary is a tedious and repeated work.

The nature of the problem lies in the variable exchanging between .NET and Python.

I think a scope class can be added to manage the context of a interaction with Python and simplify the variable exchanging.
A Scope should act like a Python Module in .NET. In the Scope class, many python functions can be wrapped to operate with the locals and globals for easy-to-use and speeding up.

For example, the eval example provided in that PR,

dynamic sys = Py.Import("sys");sys.attr1 = 100;PyDict locals = new PyDict();locals.SetItem("sys", sys);locals.SetItem("a", new PyInt(10)); PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals.Handle);var c = locals.GetItem("c").AsManagedObject(typeof(Int32));Console.WriteLine("c= {0}", c);

can be written as,

var ps = Py.Session('test')sys = ps.Import("sys");sys.attr1 = 100;ps.SetLocal("a", 10);ps.Exec("c = sys.attr1 + a + 1");   //passvar c = ps.Get<System.Int32>("c");  //get the local variable and convert it to .NET type.ps.Dispose(); orps.Suspend(); //use it later. Py.Session('test') will return it.

I implement a prototype of this proposalhere to help discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp