Python – function __main__.add_numbers(x, y) in __main__

in __main__… here is a solution to the problem.

in __main__

Suppose such a function:

 In [56]: def add_numbers(x, y): return x + y

When I use it without parentheses

In [57]: add_numbers
Out[57]: <function __main__.add_numbers(x, y)>

What is the __main__ here for?
It is not in the standard or meta properties of add_numbers:

In [59]: "__main__" in dir(add_numbers)
Out[59]: False

Solution

What you see is the IPython feature. It uses pretty-print, which adds the module name to the function’s qualified name. If you disable pretty-print, you will get usual function repr :

>>> def add_numbers(x, y): return x + y
... 
>>> add_numbers
<function __main__.add_numbers(x, y)>
>>> %pprint
Pretty printing has been turned OFF
>>> add_numbers
<function add_numbers at 0x107921598>

The module of the function is __main__ because you have defined it interactively. If you imported from another module, you will see the module name there.

Related Problems and Solutions