Python Argument Binders

Python Argument Binders

functools.partial returns a callable wrapping a function with some or all of the arguments frozen.

import sys
import functools

print_hello = functools.partial(sys.stdout.write, "Hello world\n")

print_hello()
Hello world

The above usage is equivalent to the following lambda.

print_hello = lambda *a, **kw: sys.stdout.write("Hello world\n", *a, **kw)

How to bind arguments to given values in Python functions?

>>> from functools import partial
>>> def f(a, b, c):
... print a, b, c
...
>>> bound_f = partial(f, 1)
>>> bound_f(2, 3)
1 2 3

How to bind parameters to a function without calling it

Often this will be done with a decorator. Here is a general example:

add = lambda x,y: x+y

def wrap(outer_func, *outer_args, **outer_kwargs):
def inner_func(*inner_args, **inner_kwargs):
args = list(outer_args) + list(inner_args)
kwargs = {**outer_kwargs, **inner_kwargs}
return outer_func(*args, **kwargs)
return inner_func

In this case you can do things such as the following:

# pass both at once
>>> x=wrap(add,2,3)
>>> x()
5

# pass one at binding, second at call
>>> x=wrap(add,2)
>>> x(3)
5

# pass both when called
>>> x=wrap(add)
>>> x(2,3)
5

Note that the above is very similar to functools.partial:

The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. For example, partial() can be used to create a callable that behaves like the int() function where the base argument defaults to two:

from functools import partial
basetwo = partial(int, base=2)
basetwo.__doc__ = 'Convert base 2 string to an int.'
basetwo('10010')
18

How to bind a constant parameter in Python?

You want to use functools.partial:

import functools

instC = C(5.)
meth = functools.partial(instC.withParam, 239)

Then you can pass this method (bound with both your instance and the value 239):

do_thing(meth)

is there a way to bind parameter to a function variable in python?

Either use a functools.partial() object or a lambda expression:

from functools import partial

ptr = partial(self.testMethod, 'Joe')

or

ptr = lambda: self.testMethod('Joe')

Function restriction by fixing an argument

You can write your own function:

def g(y):
return f(2, y)

Or more concisely:

g = lambda y: f(2, y)

There's also functools.partial:

import functools

def f(x, y):
return x + y

g = functools.partial(f, 2)

You can then call it as before:

>>> g(3)
5

Tkinter binding a function with arguments to a widget

When you create a binding with bind, Tkinter automatically adds an argument that has information about the event. You'll need to account for that either in your rand_func definition or in how you call it.

This argument is not included when you use the command attribute. You must take care to account for this extra argument either in how you call the function in each case, or in how the function interprets its parameters.

Here's one solution that uses lambda in the binding to accept the extra event only when using bind command, but not pass it on to the final command.

import tkinter as tk

class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack()
self.button = tk.Button(self.frame, text="click me",
command=lambda a=1, b=2, c=3:
self.rand_func(a, b, c))
self.button.pack()
self.frame.bind("<Return>",
lambda event, a=10, b=20, c=30:
self.rand_func(a, b, c))
# make sure the frame has focus so the binding will work
self.frame.focus_set()

def rand_func(self, a, b, c):
print "self:", self, "a:", a, "b:", b, "c:", c
print (a+b+c)

app = SampleApp()
app.mainloop()

That being said, it's rare that binding to a frame is the right thing to do. Typically a frame won't have keyboard focus, and unless it has focus the binding will never fire. If you are setting a global binding you should either bind to the "all" binding tag (using the bind_all method) or to the toplevel widget.

Passing functions with arguments to another function in Python?

Do you mean this?

def perform(fun, *args):
fun(*args)

def action1(args):
# something

def action2(args):
# something

perform(action1)
perform(action2, p)
perform(action3, p, r)


Related Topics



Leave a reply



Submit