How to prevent illegal definition of a variable in objects?

Daehyok Shin sdhyok at email.unc.edu
Fri Jun 16 14:18:57 EDT 2000


How can I get the DCI module?

Peter

"Fred Gansevles" <gansevle at cs.utwente.nl> wrote in message
news:8ia4tv$e19$1 at nnrp1.deja.com...
> In article <8i8ujt$hop$1 at news2.isis.unc.edu>,
>   "Daehyok Shin" <sdhyok at email.unc.edu> 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
> >
> >
>
> Some time ago I posted my DCI module. This module also has a attribute
> module wich can, among other atribute-related stuff, do this for you.
>
> For instance,
> >>> import dci.attr
> >>> class A:
> ...     def __init__(self):
> ...         dci.attr.init(self) # initialize the attribute mechanism
> ...         dci.attr.declare(self, 'x')
> ...
> >>>
> >>> a = A()
> >>> a.x = 10
> >>> a.y = 20
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "dci/attr.py",
> line 52, in __setattr__
>     raise AttributeError, "Undeclared attribute: %s" % `name`
> AttributeError: Undeclared attribute: 'y'
>
> --
> -----------------------------------------------------------------------
> ----          Linux/Windows-NT/IntraNetware Administrator          ----
> -- Whenever it sounds simple, I've probably missed something! ... Me --
> -----------------------------------------------------------------------
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.







More information about the Python-list mailing list