Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'context': 0.07; '__init__': 0.09; 'facts': 0.09; 'filename': 0.09; 'logic': 0.09; 'that).': 0.09; 'types:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '%r"': 0.16; 'early.': 0.16; 'responses.': 0.16; 'simpson': 0.16; 'somewhere.': 0.16; 'str()': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'supplied': 0.16; 'typeerror:': 0.16; 'utterly': 0.16; 'validation.': 0.16; 'value;': 0.16; 'exception': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '<': 0.19; 'passing': 0.19; 'thu,': 0.19; '>>>': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; '---': 0.24; 'cc:2**0': 0.24; '>': 0.26; '15,': 0.26; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; '"",': 0.31; 'fast.': 0.31; 'values.': 0.31; 'file': 0.32; 'another': 0.32; 'url:python': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:data': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'reports': 0.37; 'wrong': 0.37; 'skip:& 10': 0.38; 'checks': 0.38; 'pm,': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; "couldn't": 0.39; 'itself': 0.39; 'url:mail': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'simple': 0.61; 'received:': 0.65; 'to:addr:gmail.com': 0.65; 'receive': 0.70; 'therefore': 0.72; 'obvious': 0.74; 'selective': 0.84; 'responses': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=z5CXm74oj6YJabHpbqAbxLCf/rca3SAsKIdMzmKzqUs=; b=SLMcGHiLJKrzI9g2Dy5GjJkhS7V7CsLK2bx4LUV/uN0iW94D/oCMpKbmUHMewQyZTD IBNu3YXiUDPO5P7UfZdkpyt0Xl2q6EnQ8yJZPVMhxPIN0j05KnTfZea/vLbD6NLEqYv2 H36+GF4quYD5XBFjQVI3+cUxlneiLcJt7O7jUOU3dVYZ2WtO6Og2SQfRVZ5fTxO8+fQH deIW71yWilZxM2Jlmd2becQ6fBe4JV/HOctD/tHDJJA5z91/UZU8lE+emQNq+h9dq6I3 EwYaeaZwkBSkyi6TFw5sZ8H5V0xdZGSmIt5n4voWLOGPzRwcfFQpKVQkUgDz2T69bRjV Y7/A== MIME-Version: 1.0 X-Received: by 10.60.58.134 with SMTP id r6mr4681659oeq.17.1389841279638; Wed, 15 Jan 2014 19:01:19 -0800 (PST) In-Reply-To: References: <20140116012542.GA5065@cskk.homeip.net> Date: Wed, 15 Jan 2014 22:01:19 -0500 Subject: Re: data validation when creating an object From: Rita To: Chris Angelico Content-Type: multipart/alternative; boundary=089e013c71e4c4610a04f00da315 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 114 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389841289 news.xs4all.nl 2836 [2001:888:2000:d::a6]:39150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64042 --089e013c71e4c4610a04f00da315 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson 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 "", line 1, in > 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.-- --089e013c71e4c4610a04f00da315 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Unfortunately, I couldn't find the reference but I kno= w I read it somewhere. Even with a selective search I wasn't able to fi= nd 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.



--089e013c71e4c4610a04f00da315--