How can I force the use-only-after-declaration?

Laura Creighton lac at strakt.com
Thu Dec 19 08:47:22 EST 2002


If all you want to do is catch your typing errors, run pychecker on your
code.  If you really want to forbid using certain variables, then you
can get the effect you want by defining some slots via

>>> class somestruct(object):
...               def __init__(self):
...                       self.__slots__ = ['a','c']

Then when you try to use object.b you will get an attribute error.

Note: unless you absolutely know what you are doing and why you are
      doing this, you shouldn't be doing this.  Wanting to restrict
      other people from making mistakes is not enough -- if you want
      to do that, pick some other language which is better suited to
      the task.  You will drive those of us who like to add attributes on
      the fly nuts, but not actually restrict us because we know how
      to get around this as well.

Laura




More information about the Python-list mailing list