What exactly is a python variable?

BartC bc at freeuk.com
Thu Nov 17 06:37:36 EST 2016


On 17/11/2016 05:40, Veek M wrote:
> In C:
> int x = 10;
> results in storage being allocated and type and location are fixed for
> the life of the program.
>
> In Python,
> x = 10
>
> causes an object '10' to be created but how exactly is 'x' handled?
> Symbol Table lookup at compile time? Is every 'x' being substituted out
> of existence? Because type(x) gives 'int' so..

Try:

  import dis

  def fn():
  |   global x
  |   x=10

  dis.dis(fn)

(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')

This might show stuff like:

               0 LOAD_CONST               1 (10)
               3 STORE_GLOBAL             0 (x)

So already giving a better idea of what might be going on compared with 
'x=10' which is practically the same in any language.

(I'm guessing "x" is looked up at this point, created if it doesn't 
exist, and associated with the value 'integer 10'.)

-- 
Bartc



More information about the Python-list mailing list