Objects and validation

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Mon Dec 13 03:51:57 EST 2010


On 12 déc, 15:28, pyt... at lists.fastmail.net wrote:
> I have a routine in Python which is extracting information from a
> website. This information is read and inserted into objects.
>
> I currently have all the validations and checks implemented in the
> routines which are reading the HTML and creating the objects. It is
> however also possible to move all the validations into the class itself.
> What is considered the best practice for this: validation in the
> functions which read the information and creates the objects or in the
> class itself?
>

There's no one-size-fits-all answer to this question. Part of the work
really belongs to the layer that accepts data from the outside world,
part of it belong to the 'model' object itself. In any case, the
'model' object shouldn't have to worry about data conversion etc -
like, if one of the attribute is supposed to be a datetime, it's the
client code's responsability to provide a proper datetime object, not
a string that may (or not) be the textual representation of a date and
time.




More information about the Python-list mailing list