Python alternative to Javascript function.bind()? -
this question has answer here:
in javascript, 1 might write
var ids = ['item0', 'item1', 'item2', 'item3']; // variable length function click_callback(number, event) { console.log('this is: ', number); } (var k = 0; k < ids.length; k += 1) { document.getelementbyid(ids[k]).onclick = click_callback.bind(null, k); }
so can pass callback function value of k
@ time registered though has changed time function called.
does python have way equivalent?
the specific situation (but prefer general answer):
i have variable number of matplotlib plots (one each coordinate). each has spanselector, reason designed pass 2 limit points callback function. however, have same callback function. have:
def span_selected(min, max):
but need
def span_selected(which_coordinate, min, max):
at time of registering callback, of course know coordinate, need know when function called. in javascript,
callback = span_selected.bind(null, coordinate)
what in python?
turned out answered here: how bind arguments given values in python functions?
the solution is:
from functools import partial def f(a,b,c): print a,b,c bound_f = partial(f,1) bound_f(2,3)
full credits matth.
edit: in example, instead of
callback = span_selected.bind(null, coordinate)
use
callback = partial(span_selected, coordinate)
Comments
Post a Comment