Finding the instance reference of an object

Aaron "Castironpi" Brady castironpi at gmail.com
Thu Oct 16 22:38:27 EDT 2008


On Oct 16, 8:30 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 16 Oct 2008 11:51:43 -0700, Aaron \"Castironpi\" Brady wrote:
> > If you're sure it's unique, why not just scan through the pairs in
> > locals()?
>
> > for k, v in locals():
> >   if v is the_object_im_looking_for:
> >     name_im_looking_for= k
>
> > This method can sometimes return more than one name, due to the variable
> > ordering of objects in dictionaries.
>
> Because:
>
> (1) in general, objects can have no name at all, or multiple names, so
> this won't work in general (although it may work in a specific case);
>
> (2) it's awfully inefficient if you are doing it a lot; and
>
> (3) even if it works and you can afford to pay the cost, it is almost
> certainly the Wrong Way to solve the problem at hand.
>
> --
> Steven

In fact, what's more called-for, is a mapping in reverse:

name_im_looking_for= lookup[ the_object_im_looking_for ]

You'll just have to modify it in parallel with your local variables,
which is a (yet another) bad sign.



More information about the Python-list mailing list