mike bayer added the comment:

we basically need to be able to get the argument signature for anything that 
passes callable(); function, method, class, object with __call__.  So this is 
the logic we use:

import inspect

def get_callable_argspec(fn):
    if inspect.isfunction(fn) or inspect.ismethod(fn):
        inspectable = fn
    elif inspect.isclass(fn):
        inspectable = fn.__init__
    elif hasattr(fn, '__call__'):
        inspectable = fn.__call__
    else:
        inspectable = fn

    try:
        return inspect.getargspec(inspectable)
    except TypeError:
        raise

def callable1(self, x, y):
    pass

class SomeClass(object):
    def __init__(self, x, y):
        pass

    def callable2(self, x, y):
        pass

    def __call__(self, x, y):
        pass

callable3 = SomeClass
callable4 = SomeClass(2, 3)

for callable_ in (callable1, SomeClass(1, 2).callable2, callable3, callable4):
    assert callable(callable_)  # the interpreter can tell me this

    # how can it reliably tell me this? 
    assert get_callable_argspec(callable_) == (["self", "x", "y"], None, None, 
None)


If you pass a builtin like datetime.datetime.today to it, 
isfunction()/ismethod()/isclass() return false, but it does have a __call__().

I'm working around this now by just refusing to act on anything that is 
types.BuiltinMethodType or types.BuiltinFunctionType.   

Any guidance on what the proper way is to get the argument signature for any 
object that returns True for callable() would be very helpful (py2.6-3.x 
compatible).  I'm not sure if there's a stdlib call for this.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue20828>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to