0

I want to pass methods as variables. In the below method I have 3 methods that are part of the fuzz library. How can I call them using variable name.

from fuzzywuzzy import fuzz
from fuzzywuzzy import process
method_name1 ='token_sort_ratio'
method_name2 ='partial_ratio'
method_name3 ='ratio'

def compare_alg(l1, l2, alg):
    print(fuzz.alg(l1,l2))

compare_alg("Catherine M Gitau","Catherine Gitau", method_name1)
compare_alg("Catherine M Gitau","Catherine Gitau", method_name2)
compare_alg("Catherine M Gitau","Catherine Gitau", method_name3)

1 Answer 1

1

You can use getattr

from fuzzywuzzy import fuzz
from fuzzywuzzy import process
method_name1 ='token_sort_ratio'
method_name2 ='partial_ratio'
method_name3 ='ratio'

def compare_alg(l1, l2, alg):
    print(getattr(fuzz, alg)(l1,l2))

compare_alg("Catherine M Gitau","Catherine Gitau", method_name1)
compare_alg("Catherine M Gitau","Catherine Gitau", method_name2)
compare_alg("Catherine M Gitau","Catherine Gitau", method_name3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.