globals():
globals()['use_variable_as_function_name']()
use_variable_as_function_name()
: https://bytes.com/topic/python/answers/792283-calling-variable-function-name
, ( ): URL- :
l = ['condition1', 'condition2', 'condition3']
if 'condition1.' in href:
return do_something_condition1()
if 'condition2.' in href:
return do_something_condition2()
if 'condition3.' in href:
return do_something_condition3()
- 19 .
, , .
:
for e in l:
if e + '.' in href:
return globals()['do_something_' + e]()
, , .
, , , , URL:
def do_something_condition1(href):
print('========1=======' + href)
def do_something_condition2(href):
print('========2=======' + href)
def do_something_condition3(href):
print('========3=======' + href)
:
>>> 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
, :
success = '________processed successfully___________ '
def do_something_google(href):
print('========we do google-specific stuff=======')
return success + href
def do_something_bing(href):
print('========we do bing-specific stuff=======')
return success + href
def do_something_wikipedia(href):
print('========we do wikipedia-specific stuff=======')
return success + href
:
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'
:
, . .
share