Reputation: 50
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)
Upvotes: 0
Views: 85
Reputation: 2897
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)
Upvotes: 1