[SciPy-Dev] usage of inspect.getargspec ?

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Jan 5 22:09:13 EST 2012


triggered by the recent commit, I started to look a bit more at
inspect.getargspec

https://github.com/scipy/scipy/commit/c9c2d66701583984589c88c08c478e2fc6d9f4ec#L1R1213

Until now I have seen it mainly in convenience code were it makes it
easier for users but that I usually don't use, in this case it is part
of essential code.

The main problem with inspect.getargspec is that it gets easily
confused by `self` if it is an instance method and by keywords and
flexible arguments.

recent fix for curve_fit https://github.com/scipy/scipy/pull/92
my only other experience is with numpy.vectorize that has some tricky
features and IIRC has some cases of flexible arguments that don't
work.

I don't think the changes to fmin_ncg break anything in statsmodels
since we have identical simple signatures for the main functions,
hessians and gradients, but I think it would break if we add a keyword
argument to the Hessians.
It also would be possible to work around any limitations by writing a
wrapper or a lambda function.


What experience do others have with inspect?
My experience is mostly unpleasant but I never looked much at the
details of inspect, just avoided it as much as possible.
Is it ok to use it in basic library code like the optimizers?


Just asking for general comments and since the pull request is closed.

Josef
-------------- next part --------------
# -*- coding: utf-8 -*-
"""

Created on Thu Jan 05 20:19:28 2012

Author: Josef Perktold
"""

import numpy as np
import inspect

class My(object):
    def func1(self, x, p=None):
        print args
        return x
    
    @staticmethod
    def func2(x, p, z=None):
        print args
        return x
    

def func0(x):
    return x

def func1(x, p=None):
    print p
    return x

def func1a(x, p, z=None):
    print p
    return x

def func2(x, **kwds):
    print kwds
    return x

def func3(x, *args):
    print args
    return x

print 'func0', inspect.getargspec(func0)[0]    
print 'func1', inspect.getargspec(func1)[0]
print 'func1a', inspect.getargspec(func1a)[0]
print 'func2', inspect.getargspec(func2)[0]
print 'func3', inspect.getargspec(func3)[0]
my = My()
print 'class func1', inspect.getargspec(my.func1)[0]
print 'class func2', inspect.getargspec(my.func2)[0]


More information about the SciPy-Dev mailing list