Can dictionary values access their keys?
Scott David Daniels
Scott.Daniels at Acm.Org
Fri Apr 8 13:02:40 EDT 2005
Matthew Thorley wrote:
> This may be a very rudimentary question, but here goes:
From your questions, I believe you are not thinking of values as
being distinct from the names and data structures that refer to them.
What is the parent of 23 in the following expression?
1 + 11 * 2
If you know that, what is the parent of 293 in the same expression?
> 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.
def keyfor(dictionary, element):
for key, value in dictionary.iteritems():
if value == element: # value is element if identity quest
return key
raise ValueError, element
> On a similar note, if one object is part of another,
This is the idea you have wrong. In C, C++, Java, and Fortran you
might have objects part of other objects, but in Python objects refer
to each other.
How about this:
class Holder(object): pass
v = [1 + 11 * 2]
w = [1, v, 3]
d = {1: v}
o = Holder()
o.x = v
What is the parent of v?
Or even worse:
v = [1]
v[0] = v
What is the parent of v now?
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list