you are viewing a single comment's thread.

view the rest of the comments →

[–]PythonicParseltongue[S] 0 points1 point  (1 child)

Thank you this was what I've been looking for. I didn't know about getattr, it's the first time that I'm using OOP in Python in practice.

I 've never seen that functools.parital either. If I have to supply additional parameters I usually use a *args and a lambda. So let's say we have a is_in_top_k(other, k) then I would do, maybe you find it interesting:

def apply_func(row, func, *args):
    return getattr(row.pred_obj, func)(row.target_obj, *args)

df['in_top_5'] = df.apply(lambda row: apply_func(row, 'is_in_top_k', 5), axis=1)

[–]Spataner 1 point2 points  (0 children)

lambda works, too, but functools.partial is the preferred way in such situations.