Python del() built-in can't be used in assignment?
I noticed a problem when I was trying to use del in a lambda to thin out a list of threads to just those running:
map(lambda x: del(x) if not x.isAlive() else x, self.threads)
Ignore for a second that this doesn't do anything, I'm just fooling around with map, reduce, and lambda.
This fails with a syntax error at del(x). With some messing around, I think the problem is del() doesn't return a value. For example, this fails with the same error:
b = 5
x = del(b)
This doesn't, however:
def rmThis(x): del(x)
Which means I'm using this workaround:
map(lambda x: rmThis(x) if not x.isAlive() else x, self.threads)
So is the limitation just 开发者_运维技巧because del() doesn't return a value? Why not?
I'm using python 2.6.2
The limitation is that del
is a statement and not an expression. It doesn't "return a value" because statements don't return values in Python.
The lambda
form only allows you to mention expressions (because there is an implicit return
before the expression), while the def
form allows a single statement function to be specified on one line (as you have done with rmThis
).
Normally del
is used without parentheses, as:
del x
However, including parentheses around the argument as del(x)
is allowed but doesn't mean it's a function call.
Two problems. The first one is more subtle so I'll explain that first.
The issue is that del removes a variable binding. Passing it a value will not serve your purpose.
Here's an illustration
>>> a = 5
>>> del(a)
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> def mydel(x): del(x)
...
>>> a = 5
>>> mydel(a)
>>> a
5
>>>
As you can see. In the first case, the variable a
has been removed from the current namespace. There's no way to refer to the object it pointed to anymore (assuming nothing else points to the same thing).
In the second case, you are not removing a
from the namespace. You are deleting the binding of x
in the function namespace the effect of which is that you won't be able to use x
as as an rvalue anymore in the function (ie. it's an undefined variable).
The second problem is the SyntaxError
which is simpler. Python lambda
functions can only have expressions in them and not statements. del
in not an expression (i.e., not a function call) - it's a statement (the del_stmt
) and so it can't appear in the body of the lambda. You'd see the same issue if you tried putting a print
in the body of the lambda.
>>> lambda x: print x
File "<stdin>", line 1
lambda x: print x
^
SyntaxError: invalid syntax
This also accounts for why the x=del(a)
fails. The statement syntax is invalid. It's not a function that can be called.
精彩评论