Can dictionary values access their keys?
Matthew Thorley
ruach at chpc.utah.edu
Fri Apr 8 11:30:25 EDT 2005
This may be a very rudimentary question, but here goes:
If I have a simple dictionary, where the value is a class or function,
is there an interface through which it can discover what its key is?
Similar to index() for list.
For a list, assuming I new what the parent list was I could do something
like this.
>>> class child:
... def get_parent_index(self, parent):
... return parent.index(self)
...
>>> a = child()
>>> l = [a]
>>> b = l[0]
>>> b.get_parent_index(a)
>>> b.get_parent_index(l)
0
Is there a way to do something like that with dicts?
On a similar note, if one object is part of another, is there a way for
the 'child' obj to discover what/who the 'parent' object is? That way
parent does not have to be explicityly passed to get_parent_index?
The idea is like this:
>>> class child:
... def get_parent_index(self):
parent = self.magic_parent_discovery()
... return parent.index(self)
...
Thanks much
--
Matthew
More information about the Python-list
mailing list