object query assigned variable name?

John O'Hagan research at johnohagan.com
Wed May 6 01:56:35 EDT 2009


On Tue, 5 May 2009, Sion Arrowsmith wrote:
> John O'Hagan  <research at johnohagan.com> wrote:
> >I can see that it's tantalizing, though, because _somebody_ must know
> > about the assignment; after all, we just executed it!
>
> Except we haven't, if we're talking about reporting from the
>
> object's __init__:
> >>> class Brian:
>
> ...     def __init__(self):
> ...         print "I'm Brian!"
> ...
>
> >>> l = []
> >>> l[1] = Brian()
>
> I'm Brian!
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> IndexError: list assignment index out of range
>
> (Yeah, I know that's a setitem call not an assignment. Point stands.
> It also demonstrates why the whole idea of "what name is a newly-
> created object assigned to" is broken.)

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.

But even if this is true I'm guessing it'd be black magic to get to it.

Regards,

John



More information about the Python-list mailing list