Purpose of assigning the return object to the function name after the function definition

Viewed 81

I find the generated Python wrapper for a C++ function by swig has the following lines:

def my_func(arg):
    return _cpp_mod.my_cpp_func(arg)
my_func = _cpp_mod.my_cpp_func

The source code in the .i file is as follows:

%module cpp_mod
... ...
%inline %{
MyObj& my_cpp_func(arg) {
    return *new MyObj(arg);
}
%}

All functions of the generated code seem OK.
What I want to know is the purpose of the third line for the
generated python code. Thanks in advance.

1 Answers

It just the way SWIG has decided to wrap functions. The first part

def my_func(arg):
  return _cpp_mod.my_cpp_func(arg)

reveals the number of input arguments and if commments are generated, they will be inserted here.

The second part

my_func = _cpp_mod.my_cpp_func

is redefining the function to the generated library function

Related