[IronPython] Statefulness of CompiledCode

Sanghyeon Seo sanxiyn at gmail.com
Mon May 26 18:39:14 CEST 2008

2008/5/27 Greg Parker <greg.parker at brovada.com>:
> Here is a condensed version of the python script to illustrate the problem.
>  I run this from a C# application which executes the CompiledCode ten times.
>  When the length of "fooListClassVariable" is printed, it increases to ten.
>  I would have expected to see one for every iteration.
> ----SomeInclude.py------------------------
> class SomeClass:
>   fooListClassVariable = [];
>     def AddFoo(self):
>       self.fooListClassVariable.append("foo")
>       print str(len(self.fooListClassVariable))

If you want to see 1 printed for every iteration, do:

class SomeClass:
  def __init__(self):
    self.fooListClassVariable = []

etc. You want it to be an instance variable (which is initialized
every time new instance is created), not a class variable (which is
shared by all instances of the class).

Seo Sanghyeon

More information about the Ironpython-users mailing list