Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'arguments': 0.07; 'function,': 0.07; 'strings.': 0.07; '**kwargs):': 0.09; 'argument,': 0.09; 'incorrect': 0.09; 'kwargs': 0.09; 'meaningful': 0.09; 'spec': 0.09; 'subject:Function': 0.09; 'def': 0.10; 'cases': 0.15; 'dec': 0.15; 'defaults.': 0.16; 'object()': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'assignment': 0.22; 'doc': 0.22; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'instead.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; 'optional': 0.29; 'this.': 0.29; 'function': 0.30; 'like:': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'skip:k 20': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'test': 0.36; 'possible': 0.37; 'ones': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'provide': 0.62; 'loose': 0.84; 'to:name:python': 0.84; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=nuE0JfAN2gat6mBoo42hwkqFjOR/J5RG26tBK7l5t3c=; b=QM9GmwB+aJEyvr71UBlx5wD42rv3YP1el+QhK+bx1fcAbg1kTDWemjaEVgCzxKzqAM L5mO8wRWqzZFYYiBu4Mjv/4m9XGLtiboYDfFXJ0o66o8WyVQTdV/haTUtjFQ+6VYqPFm aEYIBhlS3BikklXfrJepAIjUsjoVlHEzlUnKHijbNnNrhrH0ACkLV0yH2vdUXxvkZvNS t9njMcrE2YqXQrMIp3ywl1vl6DlNZG1em3xXi4/rVWziHWjyLsl7r1wq1EW17+I9B27W xhH6e8YOmaH754MUHDpRHOs2OFlAz5IRXcFp8JoXfMW77nVZ1aoaboBDWn67h7X+2ov3 QJrg== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 27 Dec 2012 13:33:45 -0700 Subject: Re: Function Parameters To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356640464 news.xs4all.nl 6928 [2001:888:2000:d::a6]:40011 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35649 On Thu, Dec 27, 2012 at 1:16 PM, Joseph L. Casale wrote: > When you use optional named arguments in a function, how do you deal with with > the incorrect assignment when only some args are supplied? > > If I do something like: > > def my_func(self, **kwargs): > > then handle the test cases with: > > if not kwargs.get('some_key'): > raise SyntaxError > or: > > if kwargs.get('some_key') and kwargs.get('another_key'): > ... > > I loose the introspection that some IDE's provide from the doc strings. > > Any ideas on how to deal with this? Don't use kwargs for this. List out the arguments in the function spec and give the optional ones reasonable defaults. def my_func(self, some_key=None, another_key=None): if some_key and another_key: do_something() If None is a meaningful value for the argument, then a good technique is to use a unique object as the default instead. MISSING = object() def my_func(self, some_key=MISSING, another_key=MISSING): if some_key is not MISSING and another_key is not MISSING: do_something() I only use kwargs myself when the set of possible arguments is dynamic or unknown.