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


Groups > comp.lang.python > #64059 > unrolled thread

Re: data validation when creating an object

Started byCameron Simpson <cs@zip.com.au>
First post2014-01-16 17:29 +1100
Last post2014-01-16 17:29 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: data validation when creating an object Cameron Simpson <cs@zip.com.au> - 2014-01-16 17:29 +1100

#64059 — Re: data validation when creating an object

FromCameron Simpson <cs@zip.com.au>
Date2014-01-16 17:29 +1100
SubjectRe: data validation when creating an object
Message-ID<mailman.5573.1389853784.18130.python-list@python.org>
On 16Jan2014 12:46, 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:

I thought of this, but had already dispatched my message:-(
I actually thought Py2 would give me a TypeError, but I see it doesn't.
-- 
Cameron Simpson <cs@zip.com.au>

The significant problems we face cannot be solved at the same level of
thinking we were at when we created them.       - Albert Einstein

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web