Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: How much sanity checking is required for function inputs? Date: Sun, 17 Apr 2016 12:34:05 -0700 Lines: 39 Message-ID: References: <5713E52D.3060407@icloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de U9RwyuuKDsHPxE1R30RHbQCuv9RhIZH6lvCTf9Oddzqg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'value,': 0.03; 'classes,': 0.05; 'sanity': 0.07; 'strings.': 0.07; 'subject:How': 0.09; 'python.': 0.11; 'def': 0.13; 'constants': 0.16; 'coordinates': 0.16; 'degree.': 0.16; 'finer': 0.16; 'flavors': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'python?': 0.18; 'input': 0.18; 'fairly': 0.22; 'java': 0.22; 'constant': 0.22; 'code.': 0.23; 'wrote': 0.23; 'specified': 0.23; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'checking': 0.27; 'pieces': 0.27; 'received:17': 0.27; 'function': 0.28; 'values': 0.28; 'raise': 0.29; "i'm": 0.30; 'code': 0.30; 'checks': 0.30; 'position.': 0.30; 'received:10.0.0': 0.32; 'builds': 0.33; 'hell': 0.33; 'items.': 0.33; 'received:10.0': 0.34; 'list': 0.34; 'community': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'received:10': 0.37; 'two': 0.37; 'doing': 0.38; 'skip:v 20': 0.38; 'building': 0.38; 'thank': 0.38; 'to:addr:python.org': 0.40; 'header:MIME-version:1': 0.60; 'determine': 0.61; 'greetings,': 0.61; 'engine': 0.62; 'details': 0.62; 'positions': 0.64; 'college': 0.67; 'color': 0.67; 'forward.': 0.84; 'rap': 0.84; 'subject:much': 0.91; 'instructors': 0.93 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-04-17_13:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1604170297 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 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: <5713E52D.3060407@icloud.com> Xref: csiph.com comp.lang.python:107197 Greetings, I'm currently building a chess engine to learn the finer details of Python. When I learned all flavors of Java in community college a decade ago, we had to sanity check the hell out of the input values for every function and wrote a lot of redundant code in addition to the getters/setters code. Here's the input sanity checking I got for a generator function to return a set of chess pieces for a specified color and position. def generate_set(color, positions): if color not in [VARS['COLOR_BLACK'], VARS['COLOR_WHITE']]: raise Exception("Require \'{}\' or \'{}\' for input value, got \'{}\' instead.".format(VARS['COLOR_BLACK'], VARS['COLOR_WHITE'], color)) if len(positions) != 16: raise Exception("Require 16 positions for input value, got {} instead.".format(len(positions))) The two sanity checks are fairly straight forward. Color input has to match the color constant strings. Positions input has to have 16 items. I *could* sanity check the positions input to determine if it had a list of 16 valid coordinates. The reason I don't is because the code that calls this function builds the coordinates from constants with valid coordinates. However, if I was doing this in my Java classes, one of my instructors rap me on the knuckles for not sanity checking to nth degree. How much sanity checking is too much in Python? Thank you, Chris R