How to prevent illegal definition of a variable in objects?
Sean Blakey
sblakey at freei.net
Wed Jun 14 18:51:02 EDT 2000
On Wed, Jun 14, 2000 at 05:48:45PM -0400, Daehyok Shin wrote:
> How can I prevent illegal definition of a variable to an object?
> For instance,
>
> class A:
> __init__(self):
> self.x = 1
>
> __setattr__(self, name, value):
> if name == "x":
> self.x = value
> else:
> raise AttributeException
>
> __getattr__(self, name):
> if name == "x":
> return self.x
> else:
> raise AttributeException
>
> >>> a = A()
> >>> a.x = 10
> >>> a.y = 20 # I want to raise an AttributeException.
>
> Peter
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
The code you posted seems to do what you want, provided you
a)add the 'def' keyword to your function definitions
and
b) define AttributeException (perhaps you meant AttributeError?)
--
Sean Blakey, sblakey at freei.com
Software Developer, FreeInternet.com
(253)796-6500x1025
"Uncle Cosmo ... why do they call this a word processor?"
"It's simple, Skyler ... you've seen what food processors do to food,
right?"
-- MacNelley, "Shoe"
More information about the Python-list
mailing list