This question already has answers here:
Tkinter binding a function with arguments to a widget
(2 answers)
Closed 8 months ago.
I have a general question that I can't really find an answer to so hopefully you guys can help. I have a function that takes 3 parameters, below is an example of what I have.
def someFunction(self, event, string):
do stuff ..
self.canvas.bind("<Button-1>", self.someFunction("Hello"))
When I run this, I get an error saying that I passed someFunction 2 arguments instead of 3. I'm not sure why ..
Here you're binding the result of someFunction (or trying to anyway). This fails because when python tries to get the result of someFunction, it calls it only passing 1 argument ("Hello") when someFunction really expects 2 explicit arguments. You probably want something like:
self.canvas.bind('<Button-1>',lambda event: self.someFunction(event,"Hello"))
This binds a new function (which is created by lambda and wraps around self.someFunction) which passes the correct arguments.
Or,
def someFunction(self, string):
def fn(*arg)
print string
return fn
self.canvas.bind("<Button-1>",self.someFunction("Hello!"))
Related
This question already has answers here:
How to force the default parameter in python
(4 answers)
Call function without optional arguments if they are None
(10 answers)
Closed 1 year ago.
Is there a way to call a function with an argument but have the argument in a way that the default value of the function is used instead? Like
def f(val="hello"):
print(val)
def g(a=Undefined): #replace Undefined with something else
f(a)
>>> g()
hello
The reason is, the called function is not under my control and I want to inherit its default value. Of course I could
def h(a=None):
f(a) if a else f()
or even
def k(a=None):
j = lambda : f(a) if a else f()
j()
since I have to call that function a few times and also pass other parameters. But all that would be much easier if I could just tell the function I want to use its default value.
I could also simply copy paste the default value to my function, but don't want to update my function if the other one changes.
This question already has answers here:
Pass a list to a function to act as multiple arguments [duplicate]
(3 answers)
Closed 1 year ago.
I have a function and I have an array of arguments that I want to pass to the function when calling it like this:
def function(arg):
pass
function(arg)#call
but I want:
target = function
args = list()
output = call(function,args)
I know, that I can do it with Thread, but I want get a return to main Thread
Thread(target=target,args=args).start() # without output :(
The only possible solution I have come up with is
output = exec('function_name('+','.join(args))
P.S. The functions that I call have a variable number of positional and optional arguments
P.P.S. I can't edit function's code
If those are positional arguments you can use function(*args).
This question already has answers here:
Obtaining closures at runtime [duplicate]
(1 answer)
How to open a closure in python?
(5 answers)
Closed 2 years ago.
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.
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.
This question already has answers here:
How to use Python decorators to check function arguments?
(10 answers)
How do Python functions handle the types of parameters that you pass in?
(14 answers)
Closed 6 years ago.
Currently Python allows you to define functions like
def f(x, A):
I would like to be able to define functions like
def f(x: int, A: list):
because I think it would cut down on programmer errors.
How can I do this?
Right now I am resorting to
def f(x, A):
assert type(x) == int and type(A) == list, "Invalid parameters to function f"
But I think it might be easier if I could just change the signature of the def function.
Modifying def is possible, but not at all the right (simplest) approach.
The easiest way will be to use python 3. It supports the type annotations, though it does not do anything with them by default.
This answer combines python 3's annotations with a decorator and type filtering helper functions. There are plenty of decorator based solutions and recipes for type checking. Do a search for "python annotation type checking" if you want other examples.
Note that the above question includes answers with decorators for python 2 as well, if you cannot use python 3 for some reason.
This question already has answers here:
Is there a way to pass optional parameters to a function?
(5 answers)
Closed 7 years ago.
I'm making a Script with a GUI Box in a CAD program, and the User selects about 7 different surfaces in the viewport. I then pass those values onto another Function when the User hits "OK"
The function that it is passed to looks like this
def MeshingTools(od_idSurf, trgSurf, PipeBodySurf, sealSurf, threadSurf, BodySurf, cplgEndSurf):
The problem is: if the user does not need to select one of those surfaces, I get a error saying, MeshingTools() takes exactly 7 non-keyword arguments (2 given)
How can I get around this issue?
UPDATE:
I tried keyword arguments and am not quite getting what I need.
def MeshingTools(**kwargs):
print kwargs
When I just select 1 surface, I get the following out
{'PipeBodySurf': (mdb.models['FullCAL4'].rootAssembly.instances['PinNew-1'].edges[151], mdb.models['FullCAL4'].rootAssembly.instances['PinNew-1'].edges[153])}
if I try to print PipeBodySurf , it says that global name is not defined.
Any ideas?
FINAL UPDATE (SOLVED)
Now I see that **kwargs creates a dictionary, so instead of using just the parameter name in the rest of the code, you have to use kwargs['parameter'] and then it will use the values
You can use arbitrary argument passing with * operation :
def MeshingTools(*args):
for i in args:
#do stuff with i
Functions can use special argument preceded with one or two * character to collect an arbitrary number of extra arguments. (* for positional arguments and ** for keyword arguments)