>>> w=lat2W(2,2)
>>> w.neighbors
{0: [2, 1], 1: [0, 3], 2: [0, 3], 3: [1, 2]}
>>> w.cardinalities
{0: 2, 1: 2, 2: 2, 3: 2}
>>> w.neighbors[0]=[1]
>>> w.cardinalities
{0: 2, 1: 2, 2: 2, 3: 2}
>>>
we could make them both private attributes/properties but this only protects
neighbors from
being defined as another dict (or list, or string), it doesn't protect
individual elements of
neighbors (or weights). in other words if you have a private attribute say
something like:
>>> class W(object):
def __init__(self,weights):
self.__weights=weights
@property
def weights(self):
return self.__weights
>>> w=W({0:[1],1:[0,2],2:[1]})
>>> w.weights
{0: [1], 1: [0, 2], 2: [1]}
>>> w.weights={0:[1,4],1:[0,2],2:[1],4:[0]}
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
AttributeError: can't set attribute
# note that is what we want since we don't want w.weights to change. but:
>>> w.weights
{0: [1], 1: [0, 2], 2: [1]}
>>> w.weights[0]='not protected'
>>> w.weights
{0: 'not protected', 1: [0, 2], 2: [1]}
>>>
# which is definitely not what we want
Original issue reported on code.google.com by sjsrey on 28 May 2010 at 2:22
Original issue reported on code.google.com by
sjsrey
on 28 May 2010 at 2:22