开发者

python: avoiding bug with variable use before assignment in a loop

for element in container:
  # some code here
  temp_variable = f1(element)
  # more code

# much later in the code

for element in container:
  # some code
  another_variable = g(temp_variable)
  # more code
  temp_variable = f2(element)
  # more code

In the second for loop, I accidentally used v开发者_JS百科ariable temp_variable before it's assigned. Normally, I'd get NameError exception, but here unfortunately it survived, valid and initialized, from the previous loop.

Are there any coding practices, IDE tools, etc., that would help prevent such bugs?

Btw, I was thinking it might be better if variables inside the loop stayed didn't survive past the end of the loop.

EDIT

@Ignacio Vazquez-Abrams:

If I understood correctly, you suggest not to use the same variable name as a local variable in multiple loops. I have two problems with that:

  1. Often the most descriptive variable name to use happens to be the same in multiple loops. Say, I used something like unique_visitor_count. I wouldn't want to ban this variable from being used further down in the code, in another loop.

  2. When working on existing code, it would be quite onerous to check if any new variable name I want to use has already been used before.


If it's 'much later' in the code, then you should probably break the code up into multiple functions. It sounds like the function is too long.

Another tip is to use meaningful names. temp_variable, tmp, temp, etc. are not good names. Use a name that describes the value that it points to. This will eliminate a large class of potential occurrences of this problem.

Also, list comprehensions don't leak their variables so if you can use them, than that's just one more reason that they're generally better. They are not applicable to all situations though.


If you want the variables to go away after the loop ends, refactor the loop into a function. That way, after the function is called, the variables will go out of scope and will be garbage collected.

As for coding practices that would prevent such bugs, I'd recommend that you a.) put these types of functions that require temporary variables in functions, and b.) unit test (since the second for loop would not execute by itself, a unit test for just that for loop would catch your error).


Others have suggested putting code in functions if you don't want it to leak local variables. I would like to suggest that you put these functions inside a function. This way they have access to the variables defined in the outer function, so you don't have to pass them in. Just define inner functions as you need them and invoke them right away, unless of course they can be reused.

def myfunc(a):

    i = 10

    def infunc():
        for i in range(a):
            print i,
    infunc()

    print i

myfunc(5)
>>> 0 1 2 3 4 10

This isn't particularly pretty, but it does work, and you shouldn't need it very often.


Try adding del temp_variable when you no longer want it.


Try running pylint. It won't catch errors like this directly, but it will tell you when you aren't using a variable's value.


Are there any coding practices, IDE tools, etc., that would help prevent such bugs?

Design first. Code second.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜