Q re documentation Python style

John Machin sjmachin at lexicon.net
Mon Jun 9 01:31:48 CEST 2008

On Jun 9, 7:17 am, kj <so... at 987jk.com.invalid> wrote:

> For example, I want to document a function that
> takes a dictionary as argument, and this dictionary is expected to
> have 5 keys.  (When the number of mandatory arguments gets above
> 4, I find that it's too difficult to remember their order, so I
> resort to using a dictionary as the single argument.)

Like myfunc({'strarg': 'foo', 'intarg': 42}) ??
So the start of this function would look something like this:

def myfunc(dictarg):
   strarg = dictarg['strarg']
   intarg = dictarg['intarg']
   # the real work starts now

Why not use keyword args?

>>> def func(a1, a2):
...    print 'a1=%r a2=%r' % (a1, a2)
>>> func('foo', 42)
a1='foo' a2=42
>>> func(a1='foo', a2=42)
a1='foo' a2=42
>>> func(a2=42, a1='foo')
a1='foo' a2=42

Have you read this section in the Python tutorial: "4.7 More on
Defining Functions" (http://docs.python.org/tut/


More information about the Python-list mailing list