views:

30

answers:

1

When making use of del in a Python function, I'm getting false positives from PyFlakes telling me that the variable is undefined.

def foo(bar):
    # what if it's ham? eww
    if bar == 'ham':
        del bar
        return
    # otherwise yummy!
    print bar

The above function will return the following error:

C:\temp\test.py:7: undefined name 'bar'

Even though the function will work. Does anyone know of a patch to tweak the ast tree parsing to change how it's being handled? If this something others have run into?

+1  A: 

So what is your question? Deleting parameter names does not make any sense at all, so this is no real issue anyways ...

THC4k
The function I've given is syntactically correct, in that, for whatever reason, you can use the del operator to delete an object. However the way PyFlakes handles this is to maintain a dictionary of variables within a scope, and if a del operator is used in your code, it would in turn execute this: del self.scope[variable]In the example I've listed in the OP, the del call is immediately followed by a return, meaning that even though the variable is deleted, it can be used in cases where bar != 'ham', resulting in perfectly functioning code.I was hoping someone had experience with this
alexander