Right design to validate attributes of a class instance
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
12:24 UTC
Read the original article
Hit count: 205
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?
6....
Could you recommend a well designed\Pythonic\not verbose (on class with many attributes)\elegant approach?
© Stack Overflow or respective owner