object query assigned variable name?

Aaron Brady castironpi at gmail.com
Wed May 6 09:04:20 EDT 2009


On May 6, 12:56 am, John O'Hagan <resea... at johnohagan.com> wrote:
> On Tue, 5 May 2009, Sion Arrowsmith wrote:
> > John O'Hagan  <resea... 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

In principle, you can't get all the names an object is bound to, since
it might be a temporary value or member of a container at some time.
If it was in a dictionary, would you want its key?  Or sequence and
index?

Furthermore, we don't have a data structure for two-way lookup.
Sometimes you would want to lookup name by object, sometimes object by
name.

Lastly, it would make code significantly less readable </subjective>.



More information about the Python-list mailing list