


How to call this function through the string of function name in python
Traverse the functions in the execution list, but the function name obtained from the list is a string, so a type error will be prompted, and the string object cannot be called. What if we want the string to become a callable object? Or do you want to call module attributes and class attributes through variables? There are three ways to achieve this
1. eval()
##eval() is usually used to execute a string expression and return the value of the expression. Here it converts the string into the corresponding function. eval() is powerful but dangerous (eval is evil) and is not recommended.
2, locals() and globals()
locals() and globals() are two built-in functions of python, through which you can access local and global variables in a dictionary.
3. getattr()
getattr() is a built-in function of Python, getattr(object,name) is equivalent to to object.name, but here name can be a variable. Returns the bar method of the foo moduleThe methodcaller function under the standard library operator
##
The above is the detailed content of How to call this function through the string of function name in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C++ function call performance optimization includes two aspects: parameter passing strategy and return value type optimization. In terms of parameter passing, passing values is suitable for small objects and unmodifiable parameters, while passing references or pointers is suitable for large objects and modifiable parameters, and passing pointers is the fastest. In terms of return value optimization, small values can be returned directly, and large objects should return references or pointers. Choosing the appropriate strategy can improve function call performance.

Calling functions across modules in C++: Declare the function: Declare the function to be called in the header file of the target module. Implement function: Implement the function in the source file. Linking modules: Use a linker to link together modules containing function declarations and implementations. Call the function: Include the header file of the target module in the module that needs to be called, and then call the function.

C++ function call reflection technology allows dynamically obtaining function parameter and return value information at runtime. Use typeid(decltype(...)) and decltype(...) expressions to obtain parameter and return value type information. Through reflection, we can dynamically call functions and select specific functions based on runtime input, enabling flexible and scalable code.

There are five ways to call PHP functions: direct call, call through variable, anonymous function, function pointer and reflection. By choosing the method that best suits the situation, you can optimize performance and improve code simplicity.

The function calling mechanism in C++ involves passing arguments to a function and executing its code, returning the result if one exists. There are two ways to pass parameters: pass by value (modifications are made inside the function) and pass by reference (modifications are reflected in the caller). In value passing, value modifications within the function do not affect the original value (such as printValue), while modifications in reference passing affect the original value (such as printReference).

When verifying C++ function calls in unit testing, you need to verify the following two points: Parameter passing: Use assertions to check whether the actual parameters match the expected values. Return value: Use assertions to check if the actual return value is equal to the expected value.

There are three parameter passing mechanisms for C++ function calls: call by value (copying parameter values), call by reference (passing parameter references, which can modify the original variables), and pointer passing (passing parameter pointers). The selection mechanism needs to consider parameter size, whether the original variables need to be modified, and efficiency.

In C++, preprocessor macros can be used to call functions, involving the following steps: Parameter passing: Macro parameters are enclosed in parentheses and separated by commas. Return value: Use macro parameters to specify the value to be returned and assign it to a variable. Practical case: By using macro optimization to find the function of the maximum value index in the array, the number of calculations is reduced and the efficiency is improved.
