object query assigned variable name?

Sion Arrowsmith sion at viridian.paintbox
Wed May 6 06:32:40 EDT 2009


John O'Hagan  <research at johnohagan.com> wrote:
>I guess what I meant was that if I type:
>
>brian = Brian()
>
>in the python shell and then hit return, it seems to me that _somewhere_ (in 
>the interpreter? I have no idea how it's done) it must be written that the 
>new Brian object will later be assigned the name "brian", even as the process 
>of creating the instance begins. As you've just demonstrated, the actual 
>assignment occurs afterwards.

As far as CPython goes, the parser (presumably) knows, but the
bytecode interpreter doesn't (without looking ahead), and you
can't assume you've got anything more than the bytecode.

This is what I should've done in the first place:

>>> def f():
...     messiah = Brian()
...
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_GLOBAL              0 (Brian)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (messiah)
              9 LOAD_CONST               0 (None)
             12 RETURN_VALUE
>>>

So while the information might be there, I'm not sure how you
could make use of it without going into bytecodehacks territory.

-- 
\S

   under construction




More information about the Python-list mailing list