How does Python distinguish callback function which is a member of a class?

In Python, the callback is not simply a reference to a member function. Instead, it is “bound” to the object that it refers to when it was created. So a.func creates a callable that is bound to a, and b.func creates a callable that is bound to b.

Python only needs one implementation of func() in memory, but it will probably create one or more “trampoline” functions at runtime to accomplish the binding (I’m not certain of the internal details on this, and it would differ between Python implementations anyway).

If you print id(callback_a) and id(callback_b) you will get different results, showing that they are indeed different callable objects.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)