On 2/24/2015 8:56 PM, Gregory P. Smith wrote: > inspect.getargspec(method) and inspect.signature(method) both include > the 'self' parameter but how are we to figure out from method itself > that it is actually bound and that its first parameter is expected to be > a bound instance? > > So far I've come up with: > /inspect.ismethod(method) or inspect.ismethoddescriptor(method)/ > > But that is still not quite right as it remains False in 3.4 for the > simple case of: > >>>> class A: > ... def x(self): > ... pass > ... >>>> inspect.ismethod(A.x) > False >>>> inspect.ismethoddescriptor(A.x) > False >>>> inspect.signature(A.x).parameters > mappingproxy(OrderedDict([('self', <Parameter at 0x7fdd8188eae8 'self'>)])) >>>> inspect.getargspec(A.x) > ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None) > > The above works if use it on object.__init__, but not on a method of an > uninstantiated class. (it also works on the instantiated A().x) > > Checking if /(inspect.isfunction(method) and method.__qualname__ != > method.__name__)/ perhaps but that seems questionably hacky. Is there a > right way to do this via the inspect module that I've missed? > > It seems like that'd be nice, but I don't feel like I know enough to > write up a feature request for it yet. (granted I hope nobody /wants/ > to write code that does this...)
I'm not sure if it's correct, but deep in a library of mine I have: elif type(fn) == types.MethodType: # bound method? if fn.im_self is None: # no 'self' nskip = 0 else: # need to supply 'self' nskip = 1 This is also a 2.x library. No idea if it works with 3.x. Eric. _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com