Bounds checking

Martin De Kauwe mdekauwe at gmail.com
Fri Mar 18 18:35:40 EDT 2011


> Don't check for bounds, fix any bug in the code that would set your
> values out of bounds and use asserts while debugging.
>

whilst that is a nice idea in practice this just is not a practical
solution.


> Otherwise if you really need dynamic checks, it will cost you cpu, for
> sure.

Yes I agree and I hadn't decided whether to add it or not as there
aren't any current issues. However I can see that the check would
overall be safer. I was just wondering if there was some super smartie
pants solution :P


Howeverver you could for instance override the __setatttr__ of
> state object, and call the attribute's associated function.
>
> class State(object):
>     funcTable = {
>        'foo': lambda x: x >= 0.0
>     }
>
>     def __init__(self):
>        self.foo = 0
>
>     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)
>
> Untested, however it's just an idea. I'm not even sure that would be
> less cpu consuming :D

thanks I will look at what you suggested.





More information about the Python-list mailing list