uwhpsc-2016 / homework2_solution

Solution to Homework #2
0 stars 2 forks source link

Wrapper Interface Design #1

Open cswiercz opened 8 years ago

cswiercz commented 8 years ago

What I haven’t decided on is whether the Python wrappers should adhere to the same format as the corresponding C library functions or if they should be more “Pythonic” for example, the C library version of vec_add has the signature

void vec_add(double* out, double* x, double* y, int N);

whereas I've written the Python wrapper homework2.vec_add to have the signature

def vec_add(x, y):
    # return the sum of x and y by calling the C vec_add function via ctypes

The wrapper hides the fact that memory is being edited in-place. The current definition, in fact, creates an empty solution array which is then "passed" to the underlying C function.

Would it be clearer / better to have the Python wrapper signature be identical to the C function signature? The answer to "clearer" is not necessarily the same as the answer to "better". For this class I'm not sure how much time I can spend on Python's ctypes. If I shove it under the rug then the answer might change.

Anyway, a penny for your thoughts. I think I'm in favor of the former technique as opposed to the verbose latter technique even if it does hide the C-side.

mvelegar commented 8 years ago

I am in favor of the current Python wrapper signature (the "Pythonic" version, which follows the "idiomatic" Python style guide).

cswiercz commented 8 years ago

Okay. Thanks for the feedback. I think we'll have plenty of time in class to explain the wrapper or, at least, a very simple version of the wrapper.

quantheory commented 8 years ago

The way that you've written it makes the most sense to me. My philosophy regarding foreign function interfaces is that you always want to "hide" language-specific details in a low layer as close to the foreign call as possible, whenever you can get away with it. So I think it sets a better example to write the function so that it has a Pythonic interface.

cswiercz commented 8 years ago

I agree with the "hiding" philosophy from a true software development perspective. My main concern is whether it would be confusing to the student. As long as I explain it thoroughly there shouldn't be a problem.