property collections
Ben Wolfson
wolfson at uchicago.edu
Sun May 20 13:24:27 EDT 2001
In article <g44pZKATy8B7Ewa2 at jessikat.fsnet.co.uk>, "Robin Becker"
<robin at jessikat.fsnet.co.uk> wrote:
> I'm looking for ways to spell property collection ie
>
> PC.attr1 = value1
> PC.attr2 = value2
>
> PC[i].attr1 = value
>
> ...
>
> if PC[i].attr hasn't been explicitly set then the default is found on
> the un-indexed thing.
>
> My solutions seem to involve elements which need to refer to the parent
> and thus create a loop.
I don't think this creates a loop:
class PropertyCollection:
class Properties:
def __init__(self, dict):
self.__dict__.update(dict)
def __init__(self):
self.__dict__['attrs'] = {}
self.__dict__['i_attrs'] = {}
def __getitem__(self, i):
if not self.i_attrs.has_key(i):
self.i_attrs[i] = self.Properties(self.attrs)
return self.i_attrs[i]
def __getattr__(self, k):
return self.attrs[k]
def __setattr__(self, k, v):
self.attrs[k] = v
def __delattr__(self, k):
del self.attrs[k]
--
Barnabas T. Rumjuggler
I had a son once, I ain't got no son today
My son is gone now, my son is gone, oy weh
-- Joe Frank, "Woman and Bull in a Paint Factory"
More information about the Python-list
mailing list