<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Anyways, maybe I got off to a bad start, but I'm a bit leery of the<br>
language. In my estimation it's trying to be 'too clever by half', and<br>
this coming from a veteran bash/perl programmer. I mean, free form is<br>
one thing, but too much of a good thing can be harmful to your<br>
programming health. Maybe PyChecker or PyLint will help, I don't know.<br></blockquote><div><br></div><div>There's nothing at all clever going on here, though. There just seems to be a lack of understanding of how assignment and variables operate in Python. Nothing is being changed "at a distance"; the lookup and binding semantics of Python are actually really, really simple.</div>

<div><br></div><div>Everytime you reference a name, it is first looked up in the local scope: if it's not found there, it's looked up in the global scope. That's it.</div><div><br></div><div>Everytime you assign a name, it is always assigned to the local scope.</div>

<div><br></div><div>Those are the basic rules, and that's really all you need to know for all the basic doings. Anytime you try to get the value of "state", it looks in two places -- local, then global. Anytime you try to assign the value of "state", it assigns it to the local scope.</div>

<div><br></div><div>The global scope remains unchanged in the assignment; you're not altering anything at a distance or going and messing with the class definition that lives in the global scope of the module. You've just made a local variable that shadows it.</div>

<div><br></div><div>Okay, things get slightly more complicated in two places-- if you use the 'global name' statement within a function, subsequent assignments to name will assign to the global namespace instead of the local namespace. Also, if you nest functions within each-other, there is a limited searching up that nesting of local scopes to find names. There's no other nested / lexical scoping in any other context in Python.</div>

<div><br></div><div>But, overall, things are simple. Just different. There's only two scopes or namespaces: the current function body, and the global scope. Nothing's clever going on. All assignment is local, lookups search first the local and then the global namespace. In my experience, once you grasp that it's trivial to never run into this sort of error ever again. I don't just mean finding it when you do it-- but never even writing it anymore.</div>

<div><br></div><div>Just don't shadow global names in the local namespace. It's really not a big deal.</div><div><br></div><div>--S</div></div>