Junior — Middle
Is it possible to pass a function as a parameter to another function in Python?
sobes.tech AI
Answer from AI
Yes, in Python functions are first-class objects, so they can be passed as parameters to other functions. This allows for more flexible and modular code. For example:
def greet(name):
return f"Hello, {name}!"
def call_function(func, arg):
return func(arg)
result = call_function(greet, "World")
print(result) # Outputs: Hello, World!