why del is not a function or method?

bartc bc at freeuk.com
Mon Oct 16 14:48:12 EDT 2017


On 16/10/2017 18:46, Michael Torrie wrote:
> On 10/16/2017 11:21 AM, bartc wrote:
>> del x effectively removes it from the namespace so trying to use it on
>> line 4 generates the same 'undefined' error.
>>
>> However, the byte-code still needs to be aware of x: at the time when
>> line 1 is executed, the byte-code for line 3 already exists and it will
>> need to contain a reference to x.
> 
> I'm not sure why you'd think this, as we all know that code referring to
> a name involves a dictionary lookup at runtime.  So the byte code for
> line does exist before x is defined, but it does not contain a
> "reference" to x in the manner you are thinking. Instead, all line three
> knows is that it must lookup the key "x" (as in string) in the
> dictionary of local or global objects.

Yes, the reference can be a string. So the string "x" will always exist 
in the byte-code even before the program is run, and even if the bit 
that uses x is never executed.

So if you look a file of Python code and count 288 different global 
variables, then somehow those 288 have to be identified by name in the 
binary code.

The symbol tables for them are filled in as it goes along.

I'm just saying it can be done differently. I think it already is for 
local variables, otherwise 'STORE_FAST' would require a lookup each 
time. As it is, accessing a local 'x' after 'del x' says 'referenced 
before assignment'.

-- 
bartc



More information about the Python-list mailing list