This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Jimbofbx
Recipients Jimbofbx
Date 2011-12-29.19:08:37
SpamBayes Score 9.1982e-14
Marked as misclassified No
Message-id <>
In python is currently there a way to elegantly throw an error if a variable is already in the current scope?

For example:

def longfunc(self, filename):
    FILE = open(filename);
    header = FILE.readline();
    ... bunch of code ...
    childfiles = self.children;
    for child in childfiles:
         FILE = open(child);
         header = FILE.readline();
         ... do something with header ...
    for line in FILE:
       ... etc ...

In this case, I'm accidentally overriding the old values of FILE and header, resulting in a bug. But I'm not going to catch this. I've had a couple of real life bugs due to this that were a lot more subtle and lived for months without anyone noticing the output data was slightly wrong.

This situation could be prevented if there was a way to say something along the lines of "new FILE = open(child)" or "new header = FILE.readline()" and have python throw an error to let me know that it already exists. This would also make code clearer because it allows the intended scope of a variable to become more apparent. Since "new var = something" is a syntax error, adding this functionality wouldn't break old code, as long as python would allow for 'new' (or whatever the keyword would end up being) to also be a variable name (like "new new = 1" or "new = 1")
Date User Action Args
2011-12-29 19:08:39Jimbofbxsetrecipients: + Jimbofbx
2011-12-29 19:08:39Jimbofbxsetmessageid: <>
2011-12-29 19:08:38Jimbofbxlinkissue13678 messages
2011-12-29 19:08:37Jimbofbxcreate