Here, the calling line np.pad(a, 2, pad_with)
works magically!. Not able to find the corresponding python PEP document.
>>> def pad_with(vector, pad_width, iaxis, kwargs):
... pad_value = kwargs.get('padder', 10)
... vector[:pad_width[0]] = pad_value
... vector[-pad_width[1]:] = pad_value
>>> a = np.arange(6)
>>> a = a.reshape((2, 3))
>>> np.pad(a, 2, pad_with)
array([[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 0, 1, 2, 10, 10],
[10, 10, 3, 4, 5, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10]])
What is the hidden syntax in details? When should we use or avoid its abuse?
I knew how to use default arguments and *args and **kwargs for optional arguments, but not familiar with such a case where the call np.pad(a, 2, pad_with)
does not use ()
either and still works as expected.
NOTE: It might be partially answered by this post, but the procedures when stepping inside help with understanding its usage.