I've encountered this problem with a few different major third-party libraries and frameworks now. Let me try to boil it down to the essentials:
- The API provides a class
Example
, where the constructor expects acallback
parameter. When some event occurs (due to complex logic outside my control), the API will call thecallback
function. - I have a function
modify
that accepts an instance ofExample
and calls various methods on it:def modify(it): it.enabled = True it.visible = True try: it.paint('black') except AProblemComesAlong: it.whip()
- I want to create an instance
x
ofExample
. When an event occurs that is associated withx
, thex
instance should be modified viamodify
.
Thus, I would like to bind x
as an argument to modify
, per Python Argument Binders. The problem is, it doesn't exist yet, because I am still calling the constructor:
>>> from functools import partial
>>> x = Example(callback=partial(modify, x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
Of course, I could avoid the NameError
by allowing the lambda
to look up the name later:
>>> x = Example(callback=lambda: modify(x))
but this is late binding, so it doesn't work properly if e.g. I'm in a loop and instance
is the iteration variable, or if instance
is reassigned later for any other reason.
How can I accomplish early binding of instance
to its own callback?