Turn of globals in a function?

Ron_Adam radam2 at tampabay.rr.com
Sat Mar 26 15:01:28 EST 2005


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


y = True

>>>a(False):
True

>>>b(False):
*** name error here ***


Ron_Adam

 



More information about the Python-list mailing list