Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'argument': 0.04; 'method.': 0.05; 'correct.': 0.07; 'raises': 0.07; 'referenced': 0.09; 'bug': 0.10; 'cc:addr:python-list': 0.10; 'def': 0.10; 'bonus': 0.13; 'earlier.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'typo': 0.16; 'wrote:': 0.17; 'app': 0.19; 'variable': 0.20; 'assignment': 0.22; 'cc:2**0': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'bugs': 0.27; '50,': 0.29; 'assert': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'e.g.': 0.30; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'could': 0.32; 'function.': 0.33; 'problem': 0.33; 'wrong': 0.34; 'there': 0.35; 'but': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'subject:, ': 0.61; 'back': 0.62; 'more': 0.63; 'importantly,': 0.65; 'potentially': 0.66; 'header :Reply-To:1': 0.68; 'stated': 0.69; 'carefully': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'points,': 0.84; 'received:74.208.4.194': 0.84; 'thing,': 0.84; 'reasoning': 0.91; 'subject:skip:E 10': 0.95 Date: Wed, 18 Jul 2012 12:33:01 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: Steven D'Aprano Subject: Re: Encapsulation, inheritance and polymorphism References: <3vnfd9-343.ln1@satorlaser.homedns.org> <-8SdnVrXGqie25jNnZ2dnUVZ7qKdnZ2d@bt.com> <5006b2e2$0$29978$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <5006b2e2$0$29978$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:jR3pl73DVzpdJ4sgA04+/sMr9Rruq0JhmBvZX/nHc+U hgeEiLLoUbFOEPEZBQQde01JZVMgocsMA1tXcf4khm/8vjEIhx Xk48T8RBQ0xdaMDtrvKgTwyGnwKqUW1BM1jSPC7xJlp3tc/XG0 wbDrIYxlnqm17cSReMedL+HUWDgHZoPFrIi1YiLUAad6hZBVM2 CqKcXwBVl0mZySd0O46QGM1dLCA98L07c0hJpZog+YSco9MJOp GddUNGdc+oCTFuYBG6n446piW6fQVUV4WofT1jdB+NCfhq2l2/ 3U/UjNfZI8oGXIATDyQwl5gVRnFNv5jgbJ97C0kGztZQo71JQ= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342629216 news.xs4all.nl 6902 [2001:888:2000:d::a6]:48061 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25581 On 07/18/2012 08:58 AM, Steven D'Aprano wrote: > > > > 2) To check your internal reasoning in a function or method. > > For example: > > def foo(something): > n = len(something) > x = math.sqrt(x) > # We expect that x must be less than half of n. > # E.g. n=100 gives 10 < 50, which is correct. > assert x < n//2 > process(n, x) > > > > For bonus points, can you see the mistake? The stated condition is wrong. > Without the assert, the process() code could go off and potentially > silently do the wrong thing, but the assert guards against that > possibility. And once I've had a bug report that my app raises an > AssertionError, I will go back and think more carefully about the > assertion that sqrt(n) is always less than half of n. > > There are actually two bugs in that function. One is in the assertion, but more importantly, there's a typo earlier. One that would give a traceback, so it would be caught quickly. UnboundLocalError: local variable 'x' referenced before assignment Once you change the argument of sqrt() to n, then you come to the problem you were expecting; if n has a value of 1, 2, or 3, 4, or 5 the assertion will fire. -- DaveA