Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.04; 'instance,': 0.05; 'subject:Function': 0.09; 'def': 0.15; 'library': 0.15; 'callable,': 0.16; 'certainly.': 0.16; 'module:': 0.16; 'parks': 0.16; 'subject:Checking': 0.16; 'travis': 0.16; '\xc2\xa0if': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'of.': 0.18; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'checks,': 0.23; 'parameters.': 0.23; 'pm,': 0.24; 'aug': 0.24; 'code': 0.25; 'accepting': 0.25; 'function': 0.27; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'decorators': 0.30; 'sun,': 0.30; 'url:library': 0.31; 'chris': 0.32; 'source': 0.33; 'there': 0.33; 'instead': 0.33; 'things': 0.34; 'algorithms': 0.34; 'received:209.85.161': 0.35; 'url:python': 0.36; 'checks': 0.37; 'perform': 0.37; 'using': 0.37; 'returning': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; 'called': 0.40; 'might': 0.40; 'more': 0.60; 'importantly,': 0.67; 'true;': 0.67; 'methods?': 0.84; 'sender:addr:chris': 0.84; 'false;': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=/Arr5R5n51LtzmX8adnYGRHB+m7K4utV5VqY39FFcGw=; b=IK9acuXhaIiP77NwImAE0KutQDbHMPlz6DHzJBfFXBJpzQqcJC+JSa81+l9tAlo22D noPoLqXA6xYCjwWc/h9RX+y6xb2Xm6j4/mNMLPnBsxR5+Hnmp9XTDkkwtgPKaujMzfNg yTQHb+VE7h4kpjd+L45I/R4c0Pcob2GEzM6Ow= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <5176c3dc-9270-46fe-a4d3-9dc2e9e97da5@q2g2000vbz.googlegroups.com> References: <5176c3dc-9270-46fe-a4d3-9dc2e9e97da5@q2g2000vbz.googlegroups.com> Date: Sun, 28 Aug 2011 18:21:57 -0700 X-Google-Sender-Auth: U930AwON9YQAAS4KDbt7GF80hAA Subject: Re: Checking Signature of Function Parameter From: Chris Rebert To: Travis Parks Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314580920 news.xs4all.nl 2416 [2001:888:2000:d::a6]:52743 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12367 On Sun, Aug 28, 2011 at 2:20 PM, Travis Parks wrot= e: > I am trying to write an algorithms library in Python. Most of the > functions will accept functions as parameters. For instance, there is > a function called any: > > def any(source, predicate): > =C2=A0 =C2=A0for item in source: > =C2=A0 =C2=A0 =C2=A0 =C2=A0if predicate(item): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return true; > =C2=A0 =C2=A0return false; > > There are some things I want to make sure of. 1) I want to make sure > that source is iterable. 2) More importantly, I want to make sure that > predicate is callable, accepting a thing, returning a bool. > I am more concerned with the number of parameters. That can be introspected using the `inspect` module: http://docs.python.org/library/inspect.html#inspect.getargspec > Finally, can I use decorators to automatically perform these checks, > instead of hogging the top of all my methods? Certainly. Although, as others have said, the cost-benefit ratio of adding code to perform such somewhat-redundant checks might make it not worth the trouble. Cheers, Chris