I'm new to Python and I've been working on a project that involves attributes of objects in a list --one of which is a function-- to be called at a later time. However, when I define the list the function is called without it being wanted.
For example in:
x=0
def activateitem():
if x==1:
print('item has been activated')
else:
print('function called')
item=['itemname','red',1,2,activateitem()]
x=1
The result is 'function called'. Is there a way to circumvent this without making the list a function as well? Thanks in advance!
You invoked the function and thus the result was added to the list.
If that is not what you want, you can add the function itself to the list [without invoking it with the parentheses] and then invoke it later by accessing the function within the list [by its index in the list] and then invoking it.
>>> def foo():
... print('hi')
...
>>> l = [1,2,foo]
>>> l
[1, 2, <function foo at 0x7fed39328bf8>]
>>> l[2]()
hi
Add the function to the list without calling it:
item=['itemname','red',1,2,activateitem]
Using the parenthesis calls the function.
Related
Lets say I'm given a file test.py with the following contents:
def helper():
pass
def test():
foo = [helper() for _ in range(5)]
return foo
Now in another file I want to import test. And then I want to find out what functions were called in test.test. Specifically, I want to be able to know that the function helper was called (just the function name as a string is sufficient).
Is there any way to go about doing that? (no string parsing please, as you can get around it by having a comment for example)
so basically when helper() is executed it returns a string in this case 'helper', so now when you execute the function in the list comprehension it returns that string (in this case 5 times) so if u printed test() and it returned foo u would get a list of 'helper' however if u just returned first item in foo like this foo[0] it would return 'helper'
def helper():
return 'helper'
def test():
foo = [helper() for _ in range(5)]
return foo[0]
print(test())
I want to write a wrapper function which call one function and pass the results to another function. The arguments and return types of the functions are the same, but I have problem with returning lists and multiple values.
def foo():
return 1,2
def bar():
return (1,2)
def foo2(a,b):
print(a,b)
def bar2(p):
a,b=p
print(a,b)
def wrapper(func,func2):
a=func()
func2(a)
wrapper(bar,bar2)
wrapper(foo,foo2)
I am searching for a syntax which works with both function pairs to use it in my wrapper code.
EDIT: The definitions of at least foo2 and bar2 should stay this way. Assume that they are from an external library.
There is no distinction. return 1,2 returns a tuple. Parentheses do not define a tuple; the comma does. foo and bar are identical.
As I overlooked until JacobIRR's comment, your problem is that you need to pass an actual tuple, not the unpacked values from a tuple, to bar2:
a = foo()
foo2(*a)
a = bar()
bar2(a)
I don't necessarily agree with the design, but following your requirements in the comments (the function definitions can't change), you can write a wrapper that tries to execute each version (packed vs. unpacked) since it sounds like you might not know what the function expects. The wrapper written below, argfixer, does exactly that.
def argfixer(func):
def wrapper(arg):
try:
return func(arg)
except TypeError:
return func(*arg)
return wrapper
def foo():
return 1,2
def bar():
return (1,2)
#argfixer
def foo2(a,b):
print(a,b)
#argfixer
def bar2(p):
a,b=p
print(a,b)
a = foo()
b = bar()
foo2(a)
foo2(b)
bar2(a)
bar2(b)
However, if you aren't able to put the #argfixer on the line before the function definitions, you could alternatively wrap them like this in your own script before calling them:
foo2 = argfixer(foo2)
bar2 = argfixer(bar2)
And as mentioned in previous comments/answers, return 1,2 and return (1,2) are equivalent and both return a single tuple.
This code does not run because of arg differences. It runs if you use def foo2(*args): and def bar2(*p):.
The return 1, 2 and return (1, 2) are equivalent. The comma operator just creates a tuple, whether it is enclosed in parentheses or not.
All programming languages that I know of return a single value, so, since you want to return multiple, those values must be wrapped into a collection type, in this case, a tuple.
The problem is in the way you call the second function. Make it bar2(a) instead of bar2(*a), which breaks the tuple into separate arguments.
I am using a module for a project, I have to pass a function to the module and the model does something like:
class module:
def __init__(self, function, dictionary):
# dictionary is {'x':2, 'y':4, 'z':23}
function(**dictionary)
And my function is something like:
def function(*foo):
return sum(foo)
The problem is that, the module needs named variables, and will pass it to the function like an unpacked dictionary, and the number of elements in dictionary can be variable, so I cannot pre-write the function as def function(x,y,z): return sum(x,y,z), and this raises an error. I do not wish to modify the module, because then, the code will not be universal. How can I solve this problem by just changing my code?
EDIT: I need foo as a list to use in the function
You module that you can't change is calling your function with:
function(**dictionary)
You won't be able to write your function to the argument is a list — it's not being passed a list. You can accept the keywords as a dict and the easily make a list. Your function just needs to be prepared to be called that way:
def f(**foo):
This leads to:
class module:
def __init__(self, function, dictionary):
# dictionary is {'x':2, 'y':4, 'z':23}
function(**dictionary)
def f(**foo):
print(sum(foo.values()))
module(f, {'x':2, 'y':4, 'z':23})
# prints 29 as expected
def function(*args,**Kwargs):
try:
return sum(*args)
else:
return sum(**kwargs.values())
double * unpacked dictionary values, and one * is to unpacked anything(except dictionary).
The number and type of arguments are determined by code of function init
In your case this a single argument of type dictionary. So you have always to pass such function f(x) where x is a dictionary.
So the that is function f that deals with the argument.
E.g.
def fsum(x): return sum(x.values())
...
__init__(fsum, {'a': 1, 'b': 2, 'c': 3})
It seems you want the sum of the values:
def __init__(self, function, dictionary):
# dictionary is {'x':2, 'y':4, 'z':23}
function(dictionary.values())
The dictionary.values() will give a list of [2, 4, 23] for your example.
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.
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.
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
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.
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)
I understand we can use dictionary mapping with functions like this:
def call_functions(input)
def function1():
print "called function 1"
def function2():
print "called function 2"
def function3():
print "called function 3"
tokenDict = {"cat":function1,
"dog":function2,
"bear":function3}
return tokenDict[input]()
But what if we want to set two functions as a value in the dictionary? Like:
tokenDict = {"cat":function1, function2
"dog":function2
....
I tried using a list like "cat":[function1, function2] but is there an easier way to do it?
When I call it, I want it to execute function1 and then function2
As per Tyler's comment, use lists throughout for consistency. Then use a list comprehension to output the result of applying each function in your list.
Here's a working example:
def call_functions(i, val=3):
def function1(x):
return x*1
def function2(x):
return x*2
def function3(x):
return x*3
tokenDict = {"cat": [function1, function2],
"dog": [function2],
"bear": [function3]}
return [f(val) for f in tokenDict[i]]
call_functions('cat') # [3, 6]
call_functions('dog') # [6]
call_functions('bear') # [9]
Side note: you should not shadow the built-in input.
What do you mean "assign two functions to a key"? For that matter, what do you mean with assigning two anythings to a dictionary key? You need to bundle them together somehow, because the entire point of a dictionary is that each key corresponds to exactly one value (if that value is a bundle, so be it). The other answer covers that.
If you want both of those functions to be executed, and you're not going to be changing this approach much in the immediate future, you could also use a wrapper function that simply calls them in sequence:
def function4:
function1()
function2()