Wednesday, 15 May 2013

scope - Externally scoped variables in IronPython delegates -


In IronPython, I can not seem to get a variable out of the scope of a scope scope. Unlike what I can do in both C # and Python.

In C #, I can do the following (customized) example:

  Public representative Bull del (Int Index); Public static zero main () {int total_count = 0; Del d = representative (int index) {total_count + = 3; Back true; }; For (int i = 4; i <6; i ++) {d.Invoke (i); } Console.light line (total_connect); // print 6}  

I can do the same thing in Python:

  total_count = 0 def Representative (index): Global total_count total_count + Range in 3X (4,6): Representative (X) print (total CPU) # Print 6  

but affecting Python variables in Python The public sector Fu {Public Representative, Bull Del (Int Index), will be crashed by C # named Named :

  
  import fu total_count = 0 def representative (index): global total_count total_count + 3 = "global name 'total_count'  

There is a way to increase total_count in the Python rep? Without changing any of C #?

< Adding an additional global line to IronPython was working in the div class = "post-text" item class = "post-text" itemprop = "text"> This function was working outside of the representative. Not required:

  Public Sector Fu {Public Representative Bull Del (Inte Index); Public Static Intu Fouit (Del the_Delitat) {int for unrelated_count = 0; (Int i = 4; i  
  import foo global total_count # this line Adding this works. Total_count = 0 def representative (index): global total_count # You also need to keep this line. Total_count + = 3 Return True unrelated_count = Foo.FooIt (Foo.Del (Representative)) Printout # Print 6  

No comments:

Post a Comment