I'm delving inside the code for WiringPi-Python for Python and I found several blocks like this:
def wiringPiSetup():
return _wiringpi2.wiringPiSetup()
wiringPiSetup = _wiringpi2.wiringPiSetup
This is a bit puzzling for me because I think that this:
def wiringPiSetup():
return _wiringpi2.wiringPiSetup()
would yield exactly the same result as this:
wiringPiSetup = _wiringpi2.wiringPiSetup
I know that the first is declaring a new function, and the second is a reference to the original function, but in the tests I've made I find they are totally equivalent. Look here:
>>> def a():
... return 4
...
>>> def a1():
... return a()
...
>>> a2 = a
>>>
>>> a1()
4
>>> a2()
4
So, why does WiringPi-Python put both when any of them will suffice?
BTW:
The file is generated by SWIG. The function definitions are indeed 'dead code', in that you can remove the function definition altogether and just leave the assignment in place.
Because the code is autogenerated, the code is somewhat inefficient. The SWIG function that generates this code, states:
if (Getattr(n, "feature:python:callback") || !have_addtofunc(n)) {
/* If there is no addtofunc directive then just assign from the extension module (for speed up) */
Printv(f_dest, name, " = ", module, ".", name, "\n", NIL);
}
so the second assignment is there to just replace the generated Python function to speed up usage.
If the function has additional Python code to add when generating (have_addtofunc()
is true when there is a docstring, a prepend or an append value) then the replacement line isn't generated.
Presumably the original function is left in place so that auto-completion tools can make use of the function signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With