[IronPython] OnVarChanged

Michael Foord fuzzyman at voidspace.org.uk
Tue Jun 20 18:59:11 CEST 2006

John Mancine wrote:
> I realize this is likely woefully naïve but I thought I'd ask anyhow. 
> I have a very specific case in my project where I would really like to know
> when a variable inside IronPython changes. Basically when I do this:
> pythonEngine.Execute("a = 3"); // Or something more complex that ends up
> changing global var 'a'
> I am looking for a way to be notified when 'a' has been assigned to a new
> value. This seems to be pretty simple for the Set/GetGlobal() calls but what
> about code that is inside of the Execute() block? Where could I look at
> adding some code myself such that I can add these hooks? I have dug through
> some of the code to find a place to add a hook but I couldn't seem to find
> anything clear cut.
> Again, I realize this is pretty simplistic and also painfully slow for any
> moderate scripting but I have a very specific case where I would like some
> widgets in C# to respond to when a variable has changed. (The watchwindow in
> visual studio would be a good parallel of what I'm needing to do -- ie. show
> a new value has been set for a particular value you are watching).
Hello John,

I may be off beam here (I'm no C# expert and know little about the 
IronPython internals) but it sounds like what you want to do is a bit at 
conflict with the way Python works.

Python doesn't have variables by value as such, it has names and objects.

In your example, 'a' isn't an object, it's a name bound to an object.

When you do :

a = 1
a = 2

it's not that the value of a changes, instead the name is rebound to a 
different object. Tracking changes to a mutable object is one thing, but 
tracking name binding is a different thing altogether.

Perhaps you could replace the ``__dict__`` attribute of a module (where 
names are stored) with a custom 'dictionary-like' object, that calls a 
callback when assignments are made ? A better approach would be to use a 
value on an object (instead of a local variable), and you can get that 
object to perform a callback when changes are made.

All the best,


> Thanks!
> John
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

More information about the Ironpython-users mailing list