why del is not a function or method?
Steve D'Aprano
steve+python at pearwood.info
Mon Oct 16 21:07:47 EDT 2017
On Tue, 17 Oct 2017 08:57 am, Thomas Jollans wrote:
> On 16/10/17 21:12, Stefan Ram wrote:
>> ram at zedat.fu-berlin.de (Stefan Ram) writes:
>>> »x = None« observably has not the same effect as »del x«:
>>
>> Paradoxically, /deleting/ a local variable which did not
>> ever exist, has the effect of creating a ghost of that
>> local variable which then will hide a global variable:
[...]
>> . Case 2: The »del x« creates a ghost »local variable 'x'«
>> which now will hide the global variable »x«:
>>
>> |>>> x = 9
>> |>>> def f():
>> |... del x
>> |... print( x )
>> |...
>> |>>> f()
>> |UnboundLocalError: local variable 'x' referenced before assignment
>>
>> .
>
> That's not what happens. The UnboundLocalError is in the del statement,
> not the print call:
That's exactly what happens! Stefan merely explains it poorly. There's no
mysterious "ghost", it is just that x is treated as a local variable instead
of a global, and you can't delete the local before it is bound to.
`del x` is treated as a form of assignment, just like `x = 123`, and makes `x`
a local variable. So in this code:
def f():
print(x)
there is no assignment to x, so it is treated as a global. But if we write:
def f():
x = 123
print(x)
the Python compiler sees the assignment to x and makes x a local variable.
Note that the rules for this are deliberately very, very simple, so this will
make x a local:
def f():
print(x) # x is local
x = 123
and even this will too:
def f():
if False:
x = 123 # dead code
print(x) # but enough to make x a local
You can replace the line "x = 123" with "del x" in any of those functions, and
you will get the same effect: x will be treated as a local.
--
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