"jeremito" <[EMAIL PROTECTED]> escribió en el mensaje 
news:[EMAIL PROTECTED]

> Please excuse me if this is obvious to others, but I can't figure it
> out.  I am subclassing dict, but want to prevent direct changing of
> some key/value pairs.  For this I thought I should override the
> __setitem__ method as such:
>        if key == 'xT':
>            raise AttributeError("""Can't change xT.  Please change,
> xF, xS, or xG""")

Why using a dictionary? I'd use a simple class with properties:

py> class Xs(object): # class names should be Uppercase
...     def __init__(self,  xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
...         self.xS = xS
...         self.xF = xF
...         self.nu = nu
...         self.xG = xG
...     xA = property(fget=lambda self: self.xG + self.xF)
...     xT = property(fget=lambda self: self.xA + self.xS)
...
py> xs = Xs(1.0, 0.95, 0.80, 0.70)
py> print xs.xG
0.8
py> print xs.xA
1.75
py> print xs.xT
2.75
py> xs.xG = 0.5
py> print xs.xA
1.45
py> print xs.xT
2.45
py> xs.xA = 1.5
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: can't set attribute
py> xs.xT = 1.2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: can't set attribute
py>

-- 
Gabriel Genellina 


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to