1

I have a class. This class has a list of functions that are to be evaluated by a different program.

class SomeClass(object):
 def __init__(self, context):
 self.functions_to_evaluate = []

There is a function that adds functions to an instance of SomeClass, via something like:

new_function = check_number(5)
SomeClassInstance.functions_to_evaluate.append(new_function)

Where check_number is a function that will check if number is greater than 10, let's say.

If I take SomeClassInstance.functions_to_evaluate and print it, I get a bunch of python objects, like so:

<some_library.check_number object at 0x07B35B90>

I am wondering if it is possible for me to extract the input given to check_number, so something like:

SomeClassInstance.functions_to_evaluate[0].python_feature() that will return "5" or whatever the input to check_number was to me.

asked Oct 17, 2018 at 16:17
9
  • 1
    Yes, it can be possible, but that depends on the definiton of check_number(). Please edit your question and add the check_number definition. Commented Oct 17, 2018 at 16:22
  • In general, though, no, the return value of a function doesn't know anything about how the function was called, which function was called, or even that it is the return value of any particular function. Commented Oct 17, 2018 at 16:25
  • new_function is not a function, it's an evaluation (i.e., the return value) of the check_number function. Commented Oct 17, 2018 at 16:27
  • 1
    The check_number function you added will always return True or False, it will never return <some_library.check_number object at 0x07B35B90>. Commented Oct 17, 2018 at 16:28
  • 2
    @RebeccaK375 the correct definition of check_number is key to what you want, since the value you want to retrieve (5) was passed to it when you called check_number(5) . We need to see inside that function, if that value was discarded or stored somewhere. Commented Oct 17, 2018 at 16:33

4 Answers 4

4

You can use the standard library functools.partial, which creates a new partially applied function *.

>>> from functools import partial
>>> def check_number(input):
... return input > 10
>>> fn = partial(check_number, 5)
>>> fn.args # this attribute gives you back the bound arguments, as a tuple.
(5,)
>>> fn() # calls the function with the bound arguments.
False

*: actually the partial object is not a function instance, but it is a callable, and from a duck-type perspective it's a function.

answered Oct 17, 2018 at 16:42
Sign up to request clarification or add additional context in comments.

Comments

1

If new_function = check_number(5) is a closure, then you can extract this value using __closure__[0].cell_contents:

Example:

def foo(x):
 def inn(y):
 return x
 return inn
s = foo(5)
print(s.__closure__[0].cell_contents)

Output:

5

answered Oct 17, 2018 at 16:29

Comments

1

I understand your confusion, but:

new_function = check_number(5)

Is calling the function, and the new_function variable gets assigned the return value of the function.

If you have this check_number function:

def check_number(input):
 return input > 10

Then it will return False, and new_function will be False. Never <some_library.check_number object at 0x07B35B90>.

If you're getting <some_library.check_number object at 0x07B35B90> then your check_number() function is returning something else.

answered Oct 17, 2018 at 16:30

Comments

1

There are probably several ways to skin this cat. But I'd observe first and foremost that you're not adding python function objects to the functions_to_evaluate list, you're adding the evaluations of functions.

You could simply add a tuple of function, args to the list:

SomeClassInstace.functions_to_evaluate.append((check_number, 5))

And then you can:

for f, args in SomeClassInstance.functions_to_evaluate:
 print(args)
answered Oct 17, 2018 at 16:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.