Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64042

Re: data validation when creating an object

References <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com> <20140116012542.GA5065@cskk.homeip.net> <CAPTjJmoRR_ih3D=vRsEpazFgQ5Cqs28xqRtwPs4a5wwRowvD0g@mail.gmail.com>
Date 2014-01-15 22:01 -0500
Subject Re: data validation when creating an object
From Rita <rmorgan466@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5563.1389841288.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Unfortunately, I couldn't find the reference but I know I read it
somewhere. Even with a selective search I wasn't able to find it. I think I
read it in context of module/class test case writing.



I will keep your responses in mind therefore I will put logic in __init__
for data validation.

thanks again for the responses.





On Wed, Jan 15, 2014 at 8:46 PM, Chris Angelico <rosuav@gmail.com> wrote:

> On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson <cs@zip.com.au> wrote:
> > However, I would also have obvious validity checks in __init__
> > itself on the supplied values. Eg:
> >
> >   def __init__(self, size, lifetime):
> >     if size < 1:
> >       raise ValueError("size must be >= 1, received: %r" % (size,))
> >     if lifetime <= 0:
> >       raise ValueError("lifetime must be > 0, received: %r" %
> (lifetime,))
> >
> > Trivial, fast. Fails early. Note that the exception reports the
> > receive value; very handy for simple errors like passing utterly
> > the wrong thing (eg a filename when you wanted a counter, or something
> > like that).
>
> With code like this, passing a filename as the size will raise TypeError
> on Py3:
>
> >>> size = "test.txt"
> >>> size < 1
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unorderable types: str() < int()
>
> Yet another advantage of Py3 :)
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
--- Get your facts first, then you can distort them as you please.--

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: data validation when creating an object Rita <rmorgan466@gmail.com> - 2014-01-15 22:01 -0500

csiph-web