Sobes.tech
Junior — Middle

Can you share an interesting aspect of Python that few people know about?

sobes.tech AI

Answer from AI

An interesting aspect of Python is the ability to use functions as first-class objects. This means that functions can be assigned to variables, passed to other functions, and returned from them.

Example:

def greet(name):
    return f"Hello, {name}!"

say_hello = greet  # assign function to a variable
print(say_hello("Alice"))  # output: Hello, Alice!

# Function that takes another function
def call_func(func, arg):
    return func(arg)

print(call_func(greet, "Bob"))  # output: Hello, Bob!

This allows creating flexible and powerful constructs, such as decorators, event handlers, and much more. Although many know about functions as objects, not everyone realizes how much this expands the language's capabilities.