Pythonのメソッドのアリティ(受け取るパラメーターの数)を知りたいのですが。今私はこれをやっています:
def arity(obj, method):
return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self
class Foo:
def bar(self, bla):
pass
arity(Foo(), "bar") # => 1
私はこれを達成できるようにしたいと思います:
Foo().bar.arity() # => 1
更新:現在、上記の関数は組み込み型で失敗します。これに関するヘルプもいただければ幸いです。
# Traceback (most recent call last):
# File "bla.py", line 10, in <module>
# print arity('foo', 'split') # =>
# File "bla.py", line 3, in arity
# return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self
# AttributeError: 'method_descriptor' object has no attribute 'func_co