On 05/08/2015 02:26 AM, Alex Kleider wrote:
On 2015-05-07 20:45, Dave Angel wrote:
You also only showed it working on module globals. (For code at
top-level, locals() returns the same as globals() )
You could also try it inside functions, where locals() really makes
sense as a name. And you could try it in a nested function where
there may very well be non-locals (eg. closure items) that aren't
local or global.
You've taken me to new territory:
http://www.shutupandship.com/2012/01/python-closures-explained.html
I wasn't familiar with 'closures' and to be truthful, still am not,
although thanks to you I am at least aware of the idea.
But more interestingly, you could try it on items of a list, or
members of a dictionary, where there's no name at all associated with
the object.
It simply returns None. I assume that's the point you are making?
(That it has no name, or perhaps more accurately expressed: there is
no name to discover.)
That's right. But depending on why you're looking, it might be worth
doing some more subtle searching. For example, you're currently looking
at all the members of a dict. But if one of those members is of type
dict, list, or tuple, you could look at its members. If you do that
recursively, you could identify a lot more objects.
Also, since you have to pass the object into the search function, you
already have one known reference. That one might not be the interesting
one, as it might have been made just for this testing. So it would be
useful if this function produced a list of *all* the references it can find.
And you could have your function work from an id() value. Given an
integer, try to find names or expressions that yield this id value.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor