Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'string.': 0.05; 'debug': 0.07; '__init__': 0.09; 'filename': 0.09; 'that).': 0.09; 'def': 0.12; '%r"': 0.16; '__init__,': 0.16; 'call)': 0.16; 'declarations': 0.16; 'early.': 0.16; 'feasible': 0.16; 'foo(object):': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'initialiser': 0.16; 'inputs': 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; 'task.': 0.16; 'utterly': 0.16; 'value;': 0.16; 'valueerror': 0.16; 'exception': 0.16; 'size,': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'cheap': 0.19; 'passing': 0.19; 'later': 0.20; '(the': 0.22; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'convenient': 0.24; 'cheers,': 0.24; 'class.': 0.26; 'pass': 0.26; 'post': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'raise': 0.29; 'errors': 0.30; 'robert': 0.30; '"do': 0.31; 'fast.': 0.31; 'values.': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; 'probably': 0.32; 'open': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'definition': 0.35; 'etc': 0.35; 'usual': 0.35; 'test': 0.35; 'received:com.au': 0.36; 'subject:data': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'example,': 0.37; 'reports': 0.37; 'wrong': 0.37; 'so,': 0.37; 'checks': 0.38; 'connections': 0.38; 'depends': 0.38; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'easy': 0.60; 'simple': 0.61; 'you.': 0.62; 'times': 0.62; 'content- disposition:inline': 0.62; 'received:': 0.65; 'bottom': 0.67; 'invalid': 0.68; 'receive': 0.70; 'obvious': 0.74; '(probably': 0.84; 'etc,': 0.84; 'is)': 0.84; 'rita': 0.84 Date: Thu, 16 Jan 2014 12:25:42 +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=My3JGUvm-Pe8QLIrIb0A: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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389835555 news.xs4all.nl 2844 [2001:888:2000:d::a6]:33507 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64034 On 15Jan2014 20:09, Rita wrote: > I would like to do some data validation when its going to a class. > > class Foo(object): > def __init__(self): > pass > > I know its frowned upon to do work in the __init__() method and only > declarations should be there. This rule of thumb does not mean "do nothing". It may be perfetly vaid to open file or database connections in __init__, etc, depending on what the object is for. The post condition of __init__ is that the object is appropriately initialised. The definition of appropriate depends on you. Data validation is very much an appropriate thing to do in __init__. If it highly recommended to validate your inputs if that is feasible and easy at __init__ time. It is far better to get a ValueError exception (the usual exception for invalid values, which an invalid initialiser certainly is) at object creation time than at some less convenient time later during use. > So, should i create a function called validateData(self) inside foo? It might be a good idea to make a (probably private) method to check for validity and/or integrity, eg: def _is_valid(self): ... checks here, return True if ok ... because that would allow you to call this at arbitrary other times if you need to debug. 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). Certainly also put a test of self._is_valid() at the bottom of __init__, at least during the debug phase. Provided _is_valid() is cheap and fast. For example, it would be appropriate to check that a filename was a string. It would probably (your call) be inappropriate to open the file or checksum its contents etc etc. Cheers, -- Cameron Simpson I distrust a research person who is always obviously busy on a task. - Robert Frosch, VP, GM Research