[issue13678] way to prevent accidental variable overriding
Eric Snow
report at bugs.python.org
Thu Dec 29 21:09:55 CET 2011
Eric Snow <ericsnowcurrently at gmail.com> added the comment:
Interesting thought, the syntax seems unnecessary. Adding new syntax to the language is something that happens rarely and only with a _lot_ of consideration.
As a slightly more verbose alternative, currently you can do this:
def fail_if_defined(*args, namespace):
for name in args:
if name in namespace:
raise AlreadyBoundError(name)
And in your code you would put the following where you cared about it:
fail_if_defined("FILE", "header", namespace=locals())
You could even drop the namespace parameter (since it's sort of boilerplate):
def fail_if_defined(*args):
namespace = inspect.currentframe().f_back.f_locals
for name in args:
if name in namespace:
raise AlreadyBoundError(name)
However, if you are going to the trouble of sticking those in place (or of selectively using a new syntax), you are likely paying attention to the the situation already, rendering either solution unnecessary.
Ultimately, this is something better addressed instead by keeping your functions small, by being a little more cautious in naming, and particularly by careful unit testing.
----------
nosy: +eric.snow
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13678>
_______________________________________
More information about the Python-bugs-list
mailing list