Sobes.tech
Junior — Middle

Is it possible to save a function in a variable and call it later?

sobes.tech AI

Answer from AI

Yes, in Python functions are first-class objects, so they can be assigned to variables, passed as arguments, and called later.

Example:

# Define a function
def greet(name):
    return f"Hello, {name}!"

# Store the function in a variable
say_hello = greet

# Call the function through the variable
print(say_hello("World"))  # Output: Hello, World!

Thus, the variable say_hello holds a reference to the greet function and can be used to invoke it.