Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: Checking function's parameters (type, value) or not ? Date: Wed, 6 Apr 2016 14:29:02 +0100 Lines: 36 Message-ID: References: <57050a05$0$4546$426a34cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de KJdyST9wWUhqR1RIkXrb/QaCfsoQEzqaTI837WpiL0tw== 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; 'from:addr:yahoo.co.uk': 0.05; 'used.': 0.05; 'tests,': 0.07; 'ast': 0.09; 'int)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'str)': 0.09; 'worse': 0.09; 'example:': 0.10; 'stored': 0.10; 'python': 0.10; 'subject:not': 0.11; 'def': 0.13; 'interpreter': 0.15; 'result.': 0.15; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:parameters': 0.16; 'subject:type': 0.16; 'url:ftp': 0.16; 'wrote:': 0.16; 'later': 0.16; 'language': 0.19; 'library': 0.20; 'lawrence': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'parameters': 0.27; 'subject: ?': 0.27; 'function': 0.28; 'crash': 0.29; 'language.': 0.32; 'subject:) ': 0.32; 'url:python': 0.33; 'running': 0.34; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'provide': 0.61; 'url:t': 0.62; 'charset:windows-1252': 0.62; 'our': 0.64; 'designers': 0.72; 'pythonistas,': 0.84; 'subject:value': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.189.93 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 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:106575 On 06/04/2016 14:07, ast wrote: > Hello > > I would like to know if it is advised or not to test > a function's parameters before running it, e.g > for functions stored on a public library ? > > Example: > > 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 ? > Please see http://ftp.dev411.com/t/python/python-list/13bhcknhan/when-to-use-assert -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence