how do you get the name of a dictionary?
Steven D'Aprano
steve at REMOVEME.cybersource.com.au
Tue Aug 22 20:37:13 EDT 2006
On Tue, 22 Aug 2006 09:34:36 -0700, jojoba wrote:
> i don't want to do anything sophisticated with this, i am really only
> looking for a TITLE for my dictionary when i throw it in a tree editor
> that i have built. And i do realize that its not possible now. I am just
> pressing a point here.
Something like this might help.
def get_object_title(object, namespace=None):
"""Returns a possible title/name for object, in the given namespace."""
if namespace is None:
namespace = globals()
try:
return object.__name__ # works for functions, classes, modules
except AttributeError:
# search the namespace
for key,value in namespace.items():
if object is value: # do NOT use ==
return key
# not found in the namespace either
# maybe an unnamed object?
return "Object ID %d" % id(object)
Hope this helps.
--
Steven D'Aprano
More information about the Python-list
mailing list