Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56240
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Variable arguments (*args, **kwargs): seeking elegance |
| Date | 2013-10-06 09:25 +0200 |
| Organization | None |
| References | <5783f76c-5565-4089-a6d5-accb0bf329c0@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.770.1381044360.18130.python-list@python.org> (permalink) |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Variable arguments (*args, **kwargs): seeking elegance John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-05 21:04 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance Peter Otten <__peter__@web.de> - 2013-10-06 09:25 +0200
Re: Variable arguments (*args, **kwargs): seeking elegance Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-06 14:34 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-07 02:43 +0000
Re: Variable arguments (*args, **kwargs): seeking elegance Skip Montanaro <skip@pobox.com> - 2013-10-07 08:52 -0500
Re: Variable arguments (*args, **kwargs): seeking elegance John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-07 09:26 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-07 09:42 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-07 21:13 +0000
Re: Variable arguments (*args, **kwargs): seeking elegance John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-07 14:49 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-07 14:12 -0700
Re: Variable arguments (*args, **kwargs): seeking elegance Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-08 01:30 -0700
csiph-web