[SciPy-dev] stats confusion

josef.pktd@gmai... josef.pktd@gmai...
Wed Jun 17 09:20:12 CDT 2009


On Wed, Jun 17, 2009 at 9:43 AM, Pauli Virtanen<pav@iki.fi> wrote:
> Wed, 17 Jun 2009 09:37:10 -0400, josef.pktd kirjoitti:
> [clip]
>> Positional arguments cannot be called as keyword arguments, that is
>> standard python.
>> (on the other hand keyword arguments can be called also as positional
>> arguments.
>
> Positional arguments can usually be called as keyword arguments in
> standard Python.
>
> def foo(a):
>    print a
>
> foo(a=5)
>
>> the signature and help has standard notation:
>>
>> eg. signature:  np.maximum(x1, x2[, out])
>>
>>>>> np.maximum(x1=[5], x2=[6])
>> Traceback (most recent call last):
>> TypeError: 'x2' is an invalid keyword to maximum
>
> The only exception are functions provided by C extension modules, and
> functions that use *args.
>

Thanks, I didn't know this is possible.

I found the explanation in the Python Reference Manual. But I don't
thinks I have ever seen it used. And it looks a bit fragile to me if
you want to refactor the argument list later by adding *args), and I
get easily TypeErrors when mixing positional and keyword arguments if
they are in the wrong sequence.
So, I don't see any gain using positional arguments as keywords.

The problem in this example is that the the generic function cdf uses
*args, **kwds, and checks the arguments and does the broadcasting
before dispatching to the private method _cdf.

Calling the private method actually works with keywords
>>> scipy.stats.chi2._cdf(1, df=2)
0.39346934028736652
>>> scipy.stats.chi2._cdf(x=1, df=2)
0.39346934028736652


I forgot to mention earlier: The generated docs are a merge between
generic and distribution specific docstrings. I never checked whether
this merge is completely correct in all cases.

Josef

> --
> Pauli Virtanen


More information about the Scipy-dev mailing list