Would it be possible to use a variable as a function name in python? For example:
list = [one, two, three]
for item in list:
def item():
some_stuff()
Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.
To use functions in Python, you write the function name (or the variable that points to the function object) followed by parentheses (to call the function). If that function accepts arguments (as most functions do), then you'll pass the arguments inside the parentheses as you call the function.
One more thing that can be called function name (though it hardly is) is the name of a variable or an attribute where reference to that function is stored.
In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.
The trick is to use globals():
globals()['use_variable_as_function_name']()
will be equivalent to
use_variable_as_function_name()
found at: George Sakkis https://bytes.com/topic/python/answers/792283-calling-variable-function-name
The following is a useful application of the above questioning I needed right now (that's why I came here): apply special functions to URLs depending on their nature:
l = ['condition1', 'condition2', 'condition3']
I used to write
if 'condition1.' in href:
return do_something_condition1()
if 'condition2.' in href:
return do_something_condition2()
if 'condition3.' in href:
return do_something_condition3()
and so on - my list has 19 members by now and keeps growing.
While investigating the subject and developing, the function code had been quite naturally part of the main function making it soon horrible to read, so relocating the working code into functions was a great relief already.
This clumsy code above can be substituted by:
for e in l: # this is my condition list
if e + '.' in href: # this is the mechanism to choose the right function
return globals()['do_something_' + e]()
This way the main code stays simple and legible no matter how long the list of conditions may grow.
Those functions corresponding to the condition labels have to be declared conventionally, of course, depending on the nature of the type of the URL in question:
def do_something_condition1(href):
# special code 1
print('========1=======' + href)
def do_something_condition2(href):
# special code 2
print('========2=======' + href)
def do_something_condition3(href):
# special code 3
print('========3=======' + href)
Test:
>>> href = 'https://google.com'
>>> for e in l:
... globals()['do_something_' + e](href)
...
========1=======https://google.com
========2=======https://google.com
========3=======https://google.com
Or, to model it closer to the above scenario:
success = '________processed successfully___________ '
def do_something_google(href):
# special code 1
print('========we do google-specific stuff=======')
return success + href
def do_something_bing(href):
# special code 2
print('========we do bing-specific stuff=======')
return success + href
def do_something_wikipedia(href):
# special code 3
print('========we do wikipedia-specific stuff=======')
return success + href
Test:
l = ['google', 'bing', 'wikipedia']
href = 'https://google.com'
def test(href):
for e in l:
if e + '.' in href:
return globals()['do_something_' + e](href)
>>> test(href)
========we do google-specific stuff=======
'________processed successfully___________ https://google.com'
Result:
Further elaboration on the problem now just amounts to augment the condition list one by one and write the corresponding functions depending on the argument. The above mechanism will pick the right one thereafter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With