Bounds checking

Mel mwilson at the-wire.com
Fri Mar 18 11:17:56 EDT 2011


Jean-Michel Pichavant wrote:
> Martin De Kauwe wrote:

> Don't check for bounds, fix any bug in the code that would set your
> values out of bounds and use asserts while debugging.
[ ... ]
>     def __setattr__(self, attribute, value):
>        if not self.funcTable.get(attribute, lambda x: True)(value):
>            sys.exit('error out of bound')
>        return object.__setattr(self, attribute, value)

Offhand, my only quibble is that sys.exit is not helpful for debugging.  
Much better to raise an error:

        if not self.funcTable.get(attribute, lambda x: True)(value):
            raise ValueError ('error out of bound')

or define a subclass of ValueError just for this purpose.  On error, the 
program will stop just as dead, but you'll get a trace.

	Mel.



More information about the Python-list mailing list