Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'causing': 0.04; 'argument': 0.05; 'definitions': 0.07; 'arguments': 0.09; 'definition,': 0.09; 'matplotlib': 0.09; 'override': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'assume': 0.14; 'attribute,': 0.16; 'behave': 0.16; 'defaults,': 0.16; 'folks,': 0.16; 'kwargs': 0.16; 'ordereddict,': 0.16; 'personally,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'trying': 0.19; 'header:User-Agent:1': 0.23; 'error': 0.23; 'sort': 0.25; 'define': 0.26; 'handling': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'class': 0.32; "i'd": 0.34; 'there': 0.35; 'found.': 0.36; 'leads': 0.36; 'raising': 0.36; 'thanks': 0.36; 'too': 0.37; 'expected': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'new': 0.61; 'john': 0.61; 'kind': 0.63; 'such': 0.63; 'worth': 0.66; 'default': 0.69; 'conflicts': 0.84; 'look.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Variable arguments (*args, **kwargs): seeking elegance Date: Sun, 06 Oct 2013 09:25:51 +0200 Organization: None References: <5783f76c-5565-4089-a6d5-accb0bf329c0@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848f6c.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381044360 news.xs4all.nl 15888 [2001:888:2000:d::a6]:50491 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56240 John Ladasky wrote: > Hi folks, > > I'm trying to make some of Python class definitions behave like the ones I > find in professional packages, such as Matplotlib. A Matplotlib class can > often have a very large number of arguments -- some of which may be > optional, some of which will assume default values if the user does not > override them, etc. Personally, I'd rather not copy that kind of interface. > I have working code which does this kind of thing. I define required > arguments and their default values as a class attribute, in an > OrderedDict, so that I can match up defaults, in order, with *args. I'm > using set.issuperset() to see if an argument passed in **kwargs conflicts > with one which was passed in *args. I use set.isdisjoint() to look for > arguments in **kwargs which are not expected by the class definition, > raising an error if such arguments are found. Why do you rely on a homebrew solution instead of actually calling the function or initializer? > Even though my code works, I'm finding it to be a bit clunky. And now, > I'm writing a new class which has subclasses, and so actually keeps the > "extra" kwargs instead of raising an error... This is causing me to > re-evaluate my original code. > > It also leads me to ask: is there a CLEAN and BROADLY-APPLICABLE way for > handling the *args/**kwargs/default values shuffle that I can study? Or > is this sort of thing too idiosyncratic for there to be a general method? > > Thanks for any pointers! inspect.getcallargs() may be worth a look.