What exactly is a python variable?
Steve D'Aprano
steve+python at pearwood.info
Thu Nov 17 19:47:02 EST 2016
On Fri, 18 Nov 2016 12:19 am, BartC wrote:
> On 17/11/2016 12:20, Steve D'Aprano wrote:
>> On Thu, 17 Nov 2016 10:37 pm, BartC wrote:
>
>>> (I don't know how to disassemble code outside a function, not from
>>> inside the same program. Outside it might be: 'python -m dis file.py')
>
>> In the most recent versions of Python, dis.dis() will also accept a
>> string:
>>
>> py> dis.dis('y = x + 1')
>> 1 0 LOAD_NAME 0 (x)
>> 3 LOAD_CONST 0 (1)
>> 6 BINARY_ADD
>> 7 STORE_NAME 1 (y)
>> 10 LOAD_CONST 1 (None)
>> 13 RETURN_VALUE
>
>
> Py2 gave me (for "y=x+1"):
>
> 0 SETUP_EXCEPT 30781 (to 30784)
> 3 STORE_SLICE+3
> 4 <49>
>
> Py3.4 works as you say but after that result I was disinclined to take
> it further!
You may have missed the bit where I said "In the most recent versions".
Python 2.7 will be interpreting the string "y=x+1" as compiled byte-code,
and disassembling it into junk.
If in doubt, just use the compile() function first.
--
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