Sam
Sam

Reputation: 446

Is there a general way to get the argument of a function in python?

thing = ThisIsAFunction(stuff)

Is there a general way to define a function that, when inputting thing as a parameter, will return stuff?

Upvotes: 0

Views: 638

Answers (5)

Dan Getz
Dan Getz

Reputation: 9136

You could find stuff after running the code in your question if, before you run that code, you first wrap your function with a wrapper that caches previous results:

class FunCache:
    def __init__(self, fun):
        self.fun = fun
        self.cache = {}

    def __call__(self, argument):
        result = self.fun(argument)
        self.cache[result] = argument
        return result

    def getLastArgument(self, result):
        return self.cache[result]

ThisIsAFunction = FunCache(ThisIsAFunction)

thing = ThisIsAFunction(stuff)
stuff == ThisIsAFunction.getLastArgument(thing)

Depending on your use case, there could be many ways you could improve on this or make it work for that case: weak cache, decorator, monkeypatching an external library, storing lists of recent arguments, etc. Of course, this is only a solution if your function is called with the argument before you need to get the argument.

Upvotes: 3

CH.
CH.

Reputation: 606

EDIT: After a meaningful discussion with @JosephSible in the comments, I would like to note that my answer applies to finding an inverse, which is not necessarily required for what the question asks for—the use case in the question would only require a left inverse to be found.

That said, for it to be possible to find the argument from a function, that function will only need to be injective and not necessarily surjective.


As others have stated, such an inverse only exists if the function is bijective, meaning it is both surjective and injective. A good and simple explanation can be found here, but effectively what is important is that the the function only returns a certain output for one and only one specific input.

For example, a function:

def abs(x):
    if x >= 0:
        return x
    else:
        return -x

would have the same output abs(x) = 1 for x = 1 or x = -1. This means that given abs and abs(x) = 1, it is not possible to determine what value of x was used.

Also, even if these conditions were fulfilled, it is not possible to use a general method to find such an inverse. Subroutines in programming are simply a sequence of instruction to determine the output from an input, and these steps are more often than not nontrivial to reverse. Computers generally can't do such "algebra" for you, so you will have to look at each function individually and determine its inverse before implementing it.

Upvotes: 1

Hùng Nguyễn
Hùng Nguyễn

Reputation: 629

As @AER said, in general, there's no inbuilt syntax that supports this kind of thing. I strongly agree with him.

However, there is a python package that support mathematical problems here, but I would still suggest you to write that inverse function by yourself.

Upvotes: 2

AER
AER

Reputation: 1531

Short answer, no.

There's no inbuilt syntax that supports this kind of thing, called inversion. The inverse of a function, is where you input the answer of the other function and it gives you the answers.

Why?

Think of the simple case of x^2

def square_it(x):
    return x**2

If you put in -1 or 1 you would get 1. How do you map it back? It requires a one to one mapping also. In mathematics this is called being injective. Since all functions don't have this it's not part of syntax.

Upvotes: 4

No:

def ThisIsAFunction(stuff):
    return 4

Now you'll always basically be calling thing(4) no matter what stuff was.

Upvotes: 1

Related Questions