Correct approach to validate attributes of an instance of class
Posted
by systempuntoout
on Stack Overflow
See other posts from Stack Overflow
or by systempuntoout
Published on 2010-05-13T08:53:16Z
Indexed on
2010/05/13
14:14 UTC
Read the original article
Hit count: 145
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?
© Stack Overflow or respective owner