On Sat, 26 Mar 2005 20:01:28 GMT, Ron_Adam <radam2 at tampabay.rr.com> wrote:
>>Is there a way to hide global names from a function or class?>>I want to be sure that a function doesn't use any global variables by>mistake. So hiding them would force a name error in the case that I>omit an initialization step. This might be a good way to quickly>catch some hard to find, but easy to fix, errors in large code blocks.>>Examples:>>def a(x):> # ...> x = y # x is assigned to global y unintentionally.> # ...> return x>>def b(x):> # hide globals somehow> # ...> x = y # Cause a name error> # ...> return x>>
If you put the above def b in e.g. a_module.py,
and do a (untested ;-)
from a_module import b
instead of defining it locally, then the global references
from b (and whatever else you import from a_module)
should be to the global dict defined for a_module (i.e., its
outermost scope), not to the globals where you do the import.
>y = True>>>>>a(False):>True
Should work if you define a in place having same scope as the y assignment
>>>>>b(False):>*** name error here ***>
UIAM it should do this if you import b as above.