Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; '__init__': 0.09; 'filename': 0.09; 'that).': 0.09; 'def': 0.12; 'jan': 0.12; '%r"': 0.16; 'early.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'supplied': 0.16; 'utterly': 0.16; 'value;': 0.16; 'exception': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'thu,': 0.19; 'to:name:python- list@python.org': 0.22; 'header:User-Agent:1': 0.23; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; 'errors': 0.30; 'code': 0.31; 'fast.': 0.31; 'values.': 0.31; 'skip:_ 10': 0.34; 'created': 0.35; 'something': 0.35; 'but': 0.35; 'received:com.au': 0.36; 'subject:data': 0.36; 'charset:us-ascii': 0.36; 'reports': 0.37; 'wrong': 0.37; 'level': 0.37; 'problems': 0.38; 'checks': 0.38; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'simple': 0.61; 'content-disposition:inline': 0.62; 'face': 0.64; 'received:': 0.65; 'receive': 0.70; 'obvious': 0.74; 'dispatched': 0.84; 'albert': 0.91 Date: Thu, 16 Jan 2014 17:29:31 +1100 From: Cameron Simpson To: "python-list@python.org" Subject: Re: data validation when creating an object MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) References: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=bpB1Wiqi c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=ZDVDAcJxPssA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=8AHkEIZyAAAA:8 a=JmSnHRSvQwUA:10 a=pGLkceISAAAA:8 a=jA5wq0MMs399XnKoPUoA:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389853784 news.xs4all.nl 2921 [2001:888:2000:d::a6]:55068 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64059 On 16Jan2014 12:46, 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: 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 The significant problems we face cannot be solved at the same level of thinking we were at when we created them. - Albert Einstein