Python3
def sample_func_1(text : str) -> str:
add_text = "Added string"
return (text + add_text)
def sample_func_2(text : str) -> str:
add_text2 = "Added string"
return (text + add_text2)
Python3
func_dict = {
'label_1' : sample_func_1,
'label_2' : sample_func_2
}
The value value of the dictionary (* dict ) is the function (method) object itself. Therefore, in order to execute a function (method), it is necessary to add parentheses ("()") to receive the argument to the value value of the dictionary ( func_dict *).
The result of calling the function (method) "itself" stored as the value value of the dictionary (* dict *) without parentheses ("()") is as follows.
Python3
func_dict['label_1']
<function sample_func_1 at 0x100bafb80>
To execute a function (method) called from a dictionary with the above code, you need to put parentheses ("()") to receive the argument.
Python3
func_dict['label_1']()
__ If * funct_dict * is a method that receives one or more arguments, an error will occur if you call the function without giving any arguments. __
The defined method (function) was a function that takes an object of type * str * as an argument. Pass an appropriate character string as an argument and execute the method (function) stored in * func_dict *.
Python3
func_dict['label_1']("vxw")
#Execution result
'vxw Added string'
Python3
func_dict
#Execution result
{'label_1': <function sample_func_1 at 0x100bafaf0>, 'label_2': <function get_text_from_ppt at 0x100bafa60>}
func_dict['label_1']
<function sample_func_1 at 0x100bafb80>
type(func_dict['label_1'])
#Execution result
<class 'function'>
type(func_dict['label_1']())
#Execution result
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sample_func_1() missing 1 required positional argument: 'text'
type(func_dict['label_1']("giho"))
#Execution result
<class 'str'>
func_dict['label_1']("giho")
#Execution result
'giho added string'
returned_value = func_dict['label_1']("giho")
returned_value
#Execution result
'giho added string'
By using the method introduced this time, it is possible to call the function (method) corresponding to the * Key * according to the value of * Key * defined in the dictionary type object.
This is useful when you need logic to switch the appropriate function (method) to be called depending on the conditions in the flow of processing data.
As a basic example, you can think of a situation where a function (method) that performs the required processing for the file is dynamically selected, called, and executed according to the contents of the extension of the read file.
The code to cut out only the text character string and acquire the data from the PDF file, Word file, and Excel file is introduced on the following Web page.
Python3
def get_text_from_txt(filename : str) -> str:
Read the text file stored in the path received as filename and read the text*str*Processing to get as an object and output as a return value
return text
def get_text_from_pdf(filename : str) -> str:
Read the PDF file stored in the path received as filename, extract the text string, and one*str*Processing to concatenate to an object and output as a return value
return text
def get_text_from_word(filename : str) -> str:
Read the Word file stored in the path received as filename, extract the text string, and one*str*Processing to concatenate to an object and output as a return value
return text
def get_text_from_excel(filename : str) -> str:
Read the Excel file stored in the path received as filename, extract the text string, and one*str*Processing to concatenate to an object and output as a return value
return text
func_dict = {
'txt' : get_text_from_txt,
'pdf' : get_text_from_pdf,
'word' : get_text_from_word,
'excel' : get_text_from_excel
}
When storing a function (method) in * Value * of the dictionary, if you put parentheses in the function, the following error will occur. If you are trying to execute a function, it will be interpreted.
PZython3
func_dict = {
'label_1' : sample_func_1(),
'label_2' : sample_func_2()
}
#Execution result
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: sample_func_1() missing 1 required positional argument: 'text'
Defining a dictionary In the code above, you can define a dictionary (no error) by giving a specific argument in the parentheses of * sample_func_1 () *.
However, the function stored in * Value * of the dictionary becomes a function that can receive only the arguments received when defining the dictionary.
__ (Dictionary definition) __
Python3
func_dict = {
'label_1' : sample_func_1("abc"),
'label_2' : sample_func_2("def")
}
__ (Calling a function in the dictionary) __
Python3
func_dict['label_1']
#Execution result
'abc added string'
Python3
func_dict['label_1']()
#Execution result
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Python3
func_dict['label_1']("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Python3
unc_dict['label_1']('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
__ Store a text string in * Val * of the dictionary and use * eval * and * exec * to call the function. __
__ However, it is not elegant because the number of strokes on the keyboard increases. __
Python3
func_dict = {
'label_1' : "sample_func_1({arg})",
'label_2' : "sample_func_2({arg})"
}
Python3
func_dict
#Execution result
{'label_1': 'sample_func_1({arg})', 'label_2': 'sample_func_2({arg})'}
func_dict['label_1']
#Execution result
'sample_func_1({arg})'
expression = func_dict['label_1'].format(arg="'123'")
expression
#Execution result
"sample_func_1('123')"
eval(expression)
#Execution result
'123 Added string'
result = eval(expression)
result
#Execution result
'123 Added string'
add_text = "result2="
text = add_text + expression
text
#Execution result
"result2=sample_func_1('123')"
exec(text)
result2
#Execution result
'123 Added string'
Recommended Posts