Meyer Buaharon
Meyer Buaharon

Reputation: 493

Reading class method definition - what is Callable?

I am relatively new to python, and I started to read the docs when using packages, but I'm having a hard time understanding some of it:

post_to_main_thread(self: open3d.cpu.pybind.visualization.gui.Application, arg0: open3d::visualization::gui::Window, arg1: Callable[[], None]) → None

the only thing here that I don't understand is the arg1 with that callable, and I can't find an explanation on it at the web.

Upvotes: 0

Views: 249

Answers (1)

Ari Cooper-Davis
Ari Cooper-Davis

Reputation: 3515

Interesting question!

So post_to_main_thread() is a method that takes 3 arguments (inputs/variables) and returns None.

Because it's a method (a function associated with a class, not just a standalone function) the first argument, self, refers to the instance of the class that the function is part of.

The other two arguments are passed within the function parentheses, as expected with a standalone function. So a call might look like this:

instance_name = open3d.visualization.gui.Application(...)
instance_name.post_to_main_thread(arg1, arg2)

arg1 should be of type open3d::visualization::gui::Window. This is an instance of the class open3d.visualization.gui.Window().

arg2 should be of type Callable(). This describes a number of built-ins that you can find details about in the documentation. To quote:

The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or an ellipsis; the return type must be a single type.

So in this case the type should be Callable[[], None], which means this should be a function that takes no input and returns None. Carrying on from our previous example, you'd pass this as an argument like so:

def my_callable:
   print('Hello, World!')
   return

instance_name.post_to_main_thread(arg1, my_callable)

Does that clear things up?

Upvotes: 1

Related Questions