Wie man die Arität einer Methode in Python herausfindetPython

Python-Programme
Anonymous
 Wie man die Arität einer Methode in Python herausfindet

Post by Anonymous »

Ich würde gerne die Arität einer Methode in Python herausfinden (die Anzahl der Parameter, die sie empfängt).

Code: Select all

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
< /code>

Ich möchte dies erreichen: < /p>

Foo().bar.arity()   # => 1
< /code>

 Update < /strong>: Im Moment fällt die obige Funktion bei integrierten Typen aus. Jede Hilfe würde auch geschätzt: < /p>

 # Traceback (most recent call last):
#   File "bla.py", line 10, in 
#     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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post