Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Checking function's parameters (type, value) or not ? Date: Wed, 6 Apr 2016 23:27:06 +1000 Lines: 32 Message-ID: References: <57050a05$0$4546$426a34cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de DB4CXc/3nos1rhN0liAQWws/63hQK31vhgiDjl7ZLT0g== 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; 'context': 0.05; 'used.': 0.05; 'append': 0.07; 'means,': 0.07; 'tests,': 0.07; 'cc:addr :python-list': 0.09; '"if': 0.09; 'ast': 0.09; 'int)': 0.09; 'str)': 0.09; 'worse': 0.09; 'python': 0.10; 'subject:not': 0.11; 'exception': 0.13; 'def': 0.13; 'wed,': 0.15; 'interpreter': 0.15; 'result.': 0.15; '2016': 0.16; 'boolean': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'input.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:parameters': 0.16; 'subject:type': 0.16; 'wrote:': 0.16; 'later': 0.16; 'string': 0.17; 'instance,': 0.18; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'do.': 0.22; 'sep': 0.22; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'external': 0.27; 'subject: ?': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; '(maybe': 0.29; 'crash': 0.29; 'raise': 0.29; "i'd": 0.31; 'probably': 0.31; 'subject:) ': 0.32; 'point': 0.33; 'received:google.com': 0.35; 'so,': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'drop': 0.38; 'mean': 0.38; 'or,': 0.38; 'why': 0.39; 'sure': 0.39; 'some': 0.40; 'your': 0.60; "you'll": 0.61; 'provide': 0.61; 'more': 0.63; 'designers': 0.72; '11:07': 0.84; 'assemble': 0.84; 'chrisa': 0.84; 'subject:value': 0.84; 'to:none': 0.91 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:cc; bh=71kcDCU6he8YNp8EdcMs2spGXVcG9Sh4z1oOGJB0UxE=; b=fAUM0s3cgb9vx1z8NUsC4rnIeLYaHlV7sj3pOrU9H8fZ2YFtT7T0yzXuW4PKbzIq2X KuewXWupE+Tlcjvk+FizDnWXzHzj3EsaHckiz1By31hCghC0RtkCqVDxNd24jWL1Nb4O NYBMHHhyhCuzrHNXsbDCKxcIgPR64jcAdmT1ylHXacmcbRqUFQaOrHOUbvY2vl58lWTQ Io1L2bpIAEoj3rH+JjL/W4NhL29TM+mn0qhZIm5EEvEodSuM+lP61WYumu+ulqrT2k2i 1cvqRvup27iGLdmK2d2tnMCM6PkEq0c7GEDGAzPLF0I0G7XLJQjWeVijnzoKWdNjE6RU haMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=71kcDCU6he8YNp8EdcMs2spGXVcG9Sh4z1oOGJB0UxE=; b=fex3BhiLu6te3BxJexcKznQrUu+eRJvoCYkglindNt7k85YexwtL4sNgrnoA9r6ot3 K8B+vVXXab7OorRGkc0iVW+dxdDoS/4lRuyp5jIiVZf90RDW0SuC2HTdxPSczErXV1nv kOQsgzFc1FTduzz5vBxHcsfyvZ6mg7NKAE+KmEcE90K8bDmvyAVM7jL//U2kkXbtW1Uu qF7ww0jdBCf9eVLnPOTlzhuviUGZTKm75Q+85cjmg/UR+4Gha3uhFuDmZJ9q4udj9waT xBZ3o1uXNAsW5OYcfcJXo/Et63rX0+zpk5nZTV6bG1Wp0hF2TVnS1YJwSXokjNbGKMya 3Z0w== X-Gm-Message-State: AD7BkJKTZlWfUF38C0Zy2LRSkFokiA9boBhImdRR1rekvTDF//KWmv/2AwoiGL15Kqvz6xoKjkmP1sxkxG0J3A== X-Received: by 10.50.43.226 with SMTP id z2mr23748087igl.94.1459949226321; Wed, 06 Apr 2016 06:27:06 -0700 (PDT) In-Reply-To: <57050a05$0$4546$426a34cc@news.free.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <57050a05$0$4546$426a34cc@news.free.fr> Xref: csiph.com comp.lang.python:106574 On Wed, Apr 6, 2016 at 11:07 PM, ast wrote: > def to_base(nber, base=16, use_af=True, sep=''): > > assert isinstance(nber, int) and nber >= 0 > assert isinstance(base, int) and base >= 2 > assert isinstance(use_af, bool) > assert isinstance(sep, str) and len(sep) == 1 > > tbc > > With these tests, you are sure that the function to_base is > well used. But it slows down the program. > Without, python interpreter may crash later in the function > or worse provide a meaningless result. > > What library designers do ? Most likely, it'll raise an exception at some point - or, even more likely, function *correctly*. For instance, why should your sep have to be a one-character string? Most likely, you'll simply append it periodically, or use sep.join(parts) to assemble everything; a multi-character string should be fine. I don't know what "use_af" means, but again, you're probably going to simply use it in a boolean context (most basically, that would mean "if use_af: do_stuff"), so truthiness/falsiness will do. So, what I'd do is: Drop the assertions. If you actually need the checks, DO NOT use assert, but actually check and raise something else (maybe ValueError). 'assert' should NEVER be used to check external input. ChrisA