I am trying to write a function outerFunc that takes a few arguments and calls an innerFunc. If innerFunc only takes one argument I can make it work, but if it has two or more arguments I can’t figure out a way to do it.
Here’s some of the things I tried (the functions are not that important, I’m more interested in learning the correct way to handle these issues)
import numpy as np def innerFunc(x, A): return A * np.cos(x) def outerFunc(func, xMin, xMax): return func(xMax) - func(xMin) def main(): # I want to use outerFunc on innerFunc, with a given A: A = 3 xMin = 1 xMax = 2 # result = outerFunc( func = innerFunc(A = A), xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: 'x' # result2 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: 'A' # result3 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax ) # returns UnboundLocalError: cannot access local variable 'outerFunc' where it is not associated with a value def outerFunc(func, xMin, xMax, *args): # Trying to use *args return func(xMax, *args) - func(xMin, *args) # result4 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax, A = A) # returns TypeError: innerFunc() missing 1 required positional argument: 'A' def outerFunc(func, xMin, xMax, A): return func(xMax, A) - func(xMin, A) result5 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax, A = A) print(result5) # Works, but it's not very elegant # -------- if __name__ == "__main__": main()
How do I handle such cases without having to define a new outerFunc for each innerFunc?
submitted by /u/D3lta347
[link] [comments]
r/learnpython I am trying to write a function outerFunc that takes a few arguments and calls an innerFunc. If innerFunc only takes one argument I can make it work, but if it has two or more arguments I can’t figure out a way to do it. Here’s some of the things I tried (the functions are not that important, I’m more interested in learning the correct way to handle these issues) import numpy as np def innerFunc(x, A): return A * np.cos(x) def outerFunc(func, xMin, xMax): return func(xMax) – func(xMin) def main(): # I want to use outerFunc on innerFunc, with a given A: A = 3 xMin = 1 xMax = 2 # result = outerFunc( func = innerFunc(A = A), xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: ‘x’ # result2 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: ‘A’ # result3 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax ) # returns UnboundLocalError: cannot access local variable ‘outerFunc’ where it is not associated with a value def outerFunc(func, xMin, xMax, *args): # Trying to use *args return func(xMax, *args) – func(xMin, *args) # result4 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax, A = A) # returns TypeError: innerFunc() missing 1 required positional argument: ‘A’ def outerFunc(func, xMin, xMax, A): return func(xMax, A) – func(xMin, A) result5 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax, A = A) print(result5) # Works, but it’s not very elegant # ——– if __name__ == “__main__”: main() How do I handle such cases without having to define a new outerFunc for each innerFunc? submitted by /u/D3lta347 [link] [comments]
I am trying to write a function outerFunc that takes a few arguments and calls an innerFunc. If innerFunc only takes one argument I can make it work, but if it has two or more arguments I can’t figure out a way to do it.
Here’s some of the things I tried (the functions are not that important, I’m more interested in learning the correct way to handle these issues)
import numpy as np def innerFunc(x, A): return A * np.cos(x) def outerFunc(func, xMin, xMax): return func(xMax) - func(xMin) def main(): # I want to use outerFunc on innerFunc, with a given A: A = 3 xMin = 1 xMax = 2 # result = outerFunc( func = innerFunc(A = A), xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: 'x' # result2 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax ) # returns TypeError: innerFunc() missing 1 required positional argument: 'A' # result3 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax ) # returns UnboundLocalError: cannot access local variable 'outerFunc' where it is not associated with a value def outerFunc(func, xMin, xMax, *args): # Trying to use *args return func(xMax, *args) - func(xMin, *args) # result4 = outerFunc( func = innerFunc(A), xMin = xMin, xMax = xMax, A = A) # returns TypeError: innerFunc() missing 1 required positional argument: 'A' def outerFunc(func, xMin, xMax, A): return func(xMax, A) - func(xMin, A) result5 = outerFunc( func = innerFunc, xMin = xMin, xMax = xMax, A = A) print(result5) # Works, but it's not very elegant # -------- if __name__ == "__main__": main()
How do I handle such cases without having to define a new outerFunc for each innerFunc?
submitted by /u/D3lta347
[link] [comments]