why del is not a function or method?
Steve D'Aprano
steve+python at pearwood.info
Mon Oct 16 22:38:38 EDT 2017
On Tue, 17 Oct 2017 01:12 pm, Ned Batchelder wrote:
> On 10/16/17 9:06 PM, bartc wrote:
>> On 17/10/2017 01:53, Steve D'Aprano wrote:
[...]
>>> `del` is kind of like an "anti-assignment" in that the argument to
>>> `del` must
>>> be exactly the same sort of expression that can appear on the left
>>> hand side
>>> of assignment:
>>>
>>>
>>> 123 = 1+1 # illegal
>>> del 123 # also illegal
>>
>> Yet in Stefan Ram's example with del applied to a local 'x', it raised
>> an error on:
>>
>> del x # x not yet assigned to
>>
>> but an assignment to x would have been fine.
>
> Steve meant that syntactically it had to be valid on the left-hand
> side. "x" is a syntactically valid LHS, "1+1" is not.
Right.
I didn't say that "del x is a compiler declaration that has no runtime
effect", because that would have been silly. Of course del x tries to delete
the local variable x, and since x doesn't exist yet, it fails with
UnboundLocalError.
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list