File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -169,6 +169,41 @@ public void TestScopeClass()
169169 }
170170 }
171171
172+ /// <summary>
173+ /// Create a class in the scope, the class can read variables in the scope.
174+ /// Its methods can write the variables with the help of 'global' keyword.
175+ /// </summary>
176+ [ Test ]
177+ public void TestCreateVirtualPackageStructure ( )
178+ {
179+ using ( Py . GIL ( ) )
180+ {
181+ PyModule . FromString ( "scope" ,
182+ "class Class1():\n " +
183+ " def __init__(self, value):\n " +
184+ " self.value = value\n " +
185+ " def call(self, arg):\n " +
186+ " return self.value + bb + arg\n " + //use scope variables
187+ " def update(self, arg):\n " +
188+ " global bb\n " +
189+ " bb = self.value + arg\n " //update scope variable
190+ , "test"
191+ ) ;
192+
193+
194+ dynamic _ps = Py . Import ( "test.scope" ) ;
195+ _ps . bb = 100 ;
196+
197+ dynamic obj1 = _ps . Class1 ( 20 ) ;
198+ var result = obj1 . call ( 10 ) . As < int > ( ) ;
199+ Assert . AreEqual ( 130 , result ) ;
200+
201+ obj1 . update ( 10 ) ;
202+ result = ps . Get < int > ( "bb" ) ;
203+ Assert . AreEqual ( 30 , result ) ;
204+ }
205+ }
206+
172207 /// <summary>
173208 /// Import a python module into the session.
174209 /// Equivalent to the Python "import" statement.
You can’t perform that action at this time.
0 commit comments