Skip to content
Advertisement

Python passing callback function in a class

I have to try to move from the non-class-based coding style into class-based coding style, but facing an issue. The optimize() function takes a callback function mycallback(). The code works perfectly fine in Non-class-based method, but when I moved it to class-based method, I got an error “mycallback() takes exactly 3 arguments (1 given)”.

What is the right way to pass a callback function in the class-based method?

(A) Non-class-based method:

JavaScript

(B) Class-based method:

JavaScript

While this is a problem regarding passing a callback function to Gurobi’s (an optimization solver) built-in function, I believe it is a more general question on how to pass a callback function defined in a class to another function in Python.


Error For method 2:

JavaScript

Looks like it is likely to be Gurobi API issue. Wonder if any Gurobi dev will response.

Advertisement

Answer

In general, self.model.optimize(self.mycallback) should work (note: no parens after mycallback).

It may fail if the code serializes the callable e.g., to be send via pipe/socket to another process (even on different machine):

JavaScript

It works on recent Python versions where methods are pickable.

Or it may fail if model.optimize() has a bug and check for the exact function type instead of accepting any callable.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement