Correct approach to validate attributes of an instance of class
- by systempuntoout
Having a simple Python class like this:
class Spam(object):
__init__(self, description, value):
self.description = description
self.value = value
Which is the correct approach to check these constraints:
"description cannot be empty"
"value must be greater than zero"
Should i:
1.validate data before creating spam object ?
2.check data on __init__ method ?
3.create an is_valid method on Spam class and call it with spam.isValid() ?
4.create an is_valid static method on Spam class and call it with Spam.isValid(description, value) ?
5.check data on setters declaration ?
6....
Could you recommend a well designed\Pythonic\not verbose (on class with many attributes)\elegant approach?