Linked Questions

410 votes
4 answers
545k views

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'] ...
R S's user avatar
  • 11.9k
48 votes
2 answers
69k views

Is there a way to get the parameter names a function takes? def foo(bar, buz): pass magical_way(foo) == ["bar", "buz"]
Bemmu's user avatar
  • 18.3k
0 votes
3 answers
2k views

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(...
TropicalTaifun's user avatar
-1 votes
1 answer
165 views

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: ...
Zecong Hu's user avatar
  • 3,482
202 votes
9 answers
139k views

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')&...
Spì's user avatar
  • 2,295
147 votes
9 answers
31k views

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: ...
Fredrik Johansson's user avatar
120 votes
8 answers
71k views

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 ...
Phillip B Oldham's user avatar
23 votes
6 answers
21k views

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 ...
mjumbewu's user avatar
  • 1,134
18 votes
3 answers
25k views

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
JasmineOT's user avatar
  • 2,088
28 votes
3 answers
70k views

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....
user1328021's user avatar
  • 9,970
6 votes
3 answers
6k views

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, ...
user1513192's user avatar
  • 1,153
11 votes
7 answers
6k views

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 ...
QuantumFool's user avatar
14 votes
1 answer
10k views

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'...
mindthief's user avatar
  • 13.5k
15 votes
3 answers
4k views

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 ...
EnToutCas's user avatar
  • 1,387
5 votes
4 answers
2k views

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 ...
marius's user avatar
  • 1,472

15 30 50 per page