Linked Questions
40 questions linked to/from How to get method parameter names?
410 votes
4 answers
545k views
Getting list of parameter names inside python function [duplicate]
Is there an easy way to be inside a python function and get a list of the parameter names? For example: def func(a,b,c): print magic_that_does_what_I_want() >>> func() ['a','b','c'] ...
48 votes
2 answers
69k views
Python list function argument names [duplicate]
Is there a way to get the parameter names a function takes? def foo(bar, buz): pass magical_way(foo) == ["bar", "buz"]
0 votes
3 answers
2k views
How to save all parameters of a function as a string? [duplicate]
I am looking to save the input parameters of a function as a string. I am using Python 3.7. I want so save that string for debugging. What I could do easily would be something like this: def Model(...
-1 votes
1 answer
165 views
Is there a built-in way to simulate assignment of values to arguments? [duplicate]
I have a decorator that I use to suppress and log exceptions within a function. The code is something like this: def log_exceptions(func): def wrapper(*args, **kwargs): try: ...
202 votes
9 answers
139k views
How can I read a function's signature including default argument values?
Given a function object, how can I get its signature? For example, for: def my_method(first, second, third='something'): pass I would like to get "my_method(first, second, third='something')&...
147 votes
9 answers
31k views
Preserving signatures of decorated functions
Suppose I have written a decorator that does something very generic. For example, it might convert all arguments to a specific type, perform logging, implement memoization, etc. Here is an example: ...
120 votes
8 answers
71k views
Get a list/tuple/dict of the arguments passed to a function?
Given the following function: def foo(a, b, c): pass How would one obtain a list/tuple/dict/etc of the arguments passed in, without having to build the structure myself? Specifically, I'm ...
23 votes
6 answers
21k views
Is there a way to check a function's signature in Python?
I'm looking for a way to check the number of arguments that a given function takes in Python. The purpose is to achieve a more robust method of patching my classes for tests. So, I want to do ...
18 votes
3 answers
25k views
Check the number of parameters passed in Python function
I'm new in Python and wants to know if there is a simple way to get amount of passed parameters in Python function. a(1, 2, 3) ==>3 a(1, 2) ==>2
28 votes
3 answers
70k views
Python -- Only pass arguments if the variable exists
I have the following variables that a user can optionally submit through a form (they are not required, but may do this to filter down a search). color = request.GET.get ('color') size = request.GET....
6 votes
3 answers
6k views
Get Keyword Arguments for Function, Python
def thefunction(a=1,b=2,c=3): pass print allkeywordsof(thefunction) #allkeywordsof doesnt exist which would give [a,b,c] Is there a function like allkeywordsof? I cant change anything inside, ...
11 votes
7 answers
6k views
Python decorator to automatically define __init__ variables
I've got fed up of continually typing the same, repetitive commands over and over again in my __init__ function. I was wondering if I could write a decorator to do the work for me. Here's an example ...
14 votes
1 answer
10k views
How to see function signature in Python?
Is there a way to introspect a function so that it shows me information on the arguments it takes (like number of args, type if possible, name of arguments if named) and the return value? dir() doesn'...
15 votes
3 answers
4k views
Can't get argspec for Python callables?
I'm playing with Python callable. Basically you can define a python class and implement __call__ method to make the instance of this class callable. e.g., class AwesomeFunction(object): def ...
5 votes
4 answers
2k views
How to figure out which Python keyword argument is missing?
When forgetting to pass certain arguments to a function, Python gives the only-somewhat-helpful message "myfunction() takes X arguments (Y given)". Is there a way to figure out the names of the ...