Matthew Thorley wrote:
Scott David Daniels wrote: ...(explaining Python has no "the" parent)
I see what your saying, but I my situation the values of the dictionary
are unique objects created by a parent object.
But that is your convention, not Python's. Some good general rules:
  1) For frequent (or fast) access you should know where something is,
     rather than search for it.
  2) You (your app) should be willing to "pay" for your own needs for
     navigation rather than expect a built-in cost for everyone that
     provides convenience for a few.
  3) A very "Pythonic" storage system makes it clear how you navigate,
     so relying on magic is usually a bad idea.

When the 'root object' is 'global' I see what your saying, but when

class A:
  def __init__(self, container):
    self.container=container

class B(dict):
  def magice_get_parent(self):
     ...

class special_value():
  def __init__(self, val):
     self.val = val

  def magic_get_key(self):
    ...

parent = A(B)
parent.container[x] = special_value(1)
parent.container[y] = special_value(2)
parent.container[z] = special_value(1)


OK, if you want help, try doing code, not sketching what it might be.
> class special_value():
Cannot be.  Perhaps:
   class special_value(object):

> class A:
>   def __init__(self, container):
>     self.container=container
...
> class B(dict):
...
> parent = A(B)
> parent.container[x] = ...

Either you must use:
  parent = A(B())

or
  class A:
    def __init__(self, containerClass):
       self.container = containerClass()

By the way, if you are subclassing dict, you might as well
use new-style throughout (so: class A(object): ...).

Now, how about rather than all of this magic, trying:

  class A:
    def __init__(self, container):
       self.container = container
       self.container.parent = self

and instead of

parent.container[x] = special_value(1)

parent.container.set(x, special_value(1))


using: class B(dict): ... def set(self, key, contents): self[key] = contents contents.origin = self, key ...

Then after:
    parent = A(B())
    parent.container.set(x, special_value(1))
    parent.container.set(y, special_value(2))
    parent.container.set(z, special_value(1))

You can do:

    child = parent.container
    print child.parent

    value1 = parent.container[x]
    box, key = value1.origin
    assert box[key] is value1 and box.parent is parent

And you didn't have to use magic at all.

--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to