Finding the instance reference of an object
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Thu Oct 16 21:30:30 EDT 2008
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
More information about the Python-list
mailing list