0

I would like to know if there is any method to check whether two functions have the same arguments at runtime in python 3.

Basically, I have this function (func) that takes two arguments and perform some sort of computation. I want to check whether or not a and b have the same arguments' values at runtime

a = func(2, 3)
b = func(2, 3)
a.argsvalue == b.argsvalue

It is not feasible to run the code before and check the results because I am implementing a lazy framework. My main goal is to be able to understand what are the arguments of the function because there is one variable argument that I do not care but there is one static that is created before running the function.

##EDIT I actually solved this problem using the inspect module (getclosure) for those who are interested. Thank you so much for the comments it helped me to familiarize myself with the terminology. I was actually looking for the closure, which I assigned dynamically.

  • 1
    Are you willing to pass the arguments the function received in the returned value? You could write a decorator that wraps the function and changes the return value to return the arguments – Tomer Nov 18 '20 at 09:26
  • Functions don't "have" arguments; they *are called with* arguments. There is nothing you could possibly do with `a` and `b` here to get the result you want because they have nothing to do with the function any more - they are the *results of calling* the function. It is the same as if you had written `a = 1 * 6` and `b = 2 * 3` and wanted to figure out some way to look at the two `6` results and figure out whether they came from multiplying the same values together. – Karl Knechtel Nov 18 '20 at 09:27
  • 2
    What *problem do you hope to solve* by doing this, anyway? – Karl Knechtel Nov 18 '20 at 09:28
  • I might not be able to use a decorator in this case. This is because when the function gets executed is too late for my use case. Basically, I would prefer a method to simply extract the values of the arguments of an already defined/instantiated function. – Luis Enrique Pastrana Nov 18 '20 at 09:30
  • You can get this for the most part with `a = functools.partial(f, 2, 3)` then `a` is an object that has a reference to the function and its arguments which is also a callable by which the function maybe called with those arguments. – Dan D. Nov 18 '20 at 09:31

1 Answers1

0

when you do this - a.argsvalue == b.argsvalue you try to access a member of the value returned from the function.

so, if your "func" would return an object having the args you called it with (which sound like a weird thing to do) you would be able to access it.

anyway, if you need these values, just store them before sending them to the function, and then you can do whatever you want with them.

Tomer Cohen
  • 222
  • 1
  • 6