pombreda / pysal

Automatically exported from code.google.com/p/pysal
Other
0 stars 0 forks source link

weights and neighbor attributes of W are not read-only #103

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
>>> 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

GoogleCodeExporter commented 9 years ago
Here is a Read_Only_dict, that might help...

Original comment by schmi...@gmail.com on 29 May 2010 at 12:32

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by sjsrey on 29 May 2010 at 6:26