How to use a variable as function name in Python

How to use a variable as function name in Python

You can use a variable to call a function in Python using the built-in globals() or locals() dictionaries. These dictionaries hold the current global and local namespaces, respectively. Here's how you can do it:

def func1(): print("This is function 1") def func2(): print("This is function 2") # Define a variable containing the function name function_name = "func1" # Use globals() or locals() to call the function if function_name in globals() and callable(globals()[function_name]): function_to_call = globals()[function_name] function_to_call() else: print("Function not found") # Output: # This is function 1 

In this example, the variable function_name holds the name of the function you want to call. Then, using the globals() dictionary, you can check if the function name is present in the global namespace and if it's callable. If so, you retrieve the function object from globals() and call it.

Keep in mind that using globals() or locals() in this manner can be less readable and potentially less maintainable than other approaches, such as using a dictionary to map function names to function objects. Additionally, ensure that you have proper checks in place to avoid calling unintended or non-existent functions.

Examples

  1. How to call a function using a variable name in Python?

    • Description: Learn how to invoke a function dynamically by using a variable containing the function name in Python, enabling flexible and dynamic code execution.
    • Code:
      # Define functions def func1(): return "Function 1 called" def func2(): return "Function 2 called" # Function name stored in a variable function_name = "func1" # Call function using variable result = globals()[function_name]() print(result) 
  2. Python: Use a string variable as a function name

    • Description: Understand the method to use a string variable to represent a function name in Python, allowing dynamic function invocation based on runtime conditions.
    • Code:
      # Define functions def hello(): return "Hello, " def world(): return "world!" # String variable storing function name function_name = "hello" # Call function using variable result = globals()[function_name]() + world() print(result) 
  3. How to execute a function with a variable name in Python?

    • Description: Explore the approach to execute a function dynamically using a variable containing the function name in Python, facilitating adaptable program behavior.
    • Code:
      # Define functions def add(a, b): return a + b def subtract(a, b): return a - b # Variable storing function name operation = "add" # Call function based on variable result = globals()[operation](5, 3) print(result) 
  4. Dynamically calling functions with variable names in Python

    • Description: Discover the technique for dynamically invoking functions using variable names in Python, empowering you to create more versatile and extensible code structures.
    • Code:
      # Define functions def greet(): return "Hello, " def farewell(): return "Goodbye!" # Variable representing function name action = "greet" # Execute function using variable result = globals()[action]() + farewell() print(result) 
  5. Python: Invoke a function using a variable

    • Description: Learn how to invoke a function based on a variable containing the function's name in Python, facilitating dynamic function calls and runtime decision-making.
    • Code:
      # Define functions def function1(): return "Function 1 called" def function2(): return "Function 2 called" # Variable storing function name function_name = "function2" # Call function using variable result = globals()[function_name]() print(result) 
  6. How to use a variable as a function identifier in Python?

    • Description: Understand the mechanism of using a variable to identify and invoke functions in Python, enabling adaptable and responsive program behavior.
    • Code:
      # Define functions def square(x): return x ** 2 def cube(x): return x ** 3 # Variable representing function identifier operation = "square" # Execute function using variable result = globals() print(result) 
  7. Python: Call a function using a variable name stored in a string

    • Description: Explore the technique of calling a function using a string variable containing the function name in Python, facilitating dynamic function invocation.
    • Code:
      # Define functions def say_hello(): return "Hello!" def say_goodbye(): return "Goodbye!" # String variable storing function name function_name = "say_hello" # Call function using variable result = globals()[function_name]() print(result) 
  8. Dynamic function invocation using variable names in Python

    • Description: Learn how to dynamically invoke functions based on variable names in Python, providing flexibility and adaptability to your codebase.
    • Code:
      # Define functions def multiply(a, b): return a * b def divide(a, b): return a / b # Variable storing function name operation = "multiply" # Execute function using variable result = globals()[operation](6, 7) print(result) 
  9. Using a variable to call a function in Python

    • Description: Understand the technique of using a variable to call a function dynamically in Python, enabling runtime decisions and dynamic code execution.
    • Code:
      # Define functions def double(x): return x * 2 def triple(x): return x * 3 # Variable containing function name action = "double" # Call function using variable result = globals() print(result) 
  10. How to dynamically select and call a function using a variable in Python?

    • Description: Explore the method of dynamically selecting and invoking functions based on variable values in Python, enhancing the flexibility and responsiveness of your code.
    • Code:
      # Define functions def greet(): return "Hello!" def farewell(): return "Goodbye!" # Variable determining function to call action = "greet" # Call function based on variable result = globals()[action]() print(result) 

More Tags

gnu-make carousel llvm fedex android-3.0-honeycomb integer-overflow google-cloud-endpoints tomcat9 swig milliseconds

More Python Questions

More Genetics Calculators

More Other animals Calculators

More Electrochemistry Calculators

More Statistics Calculators