Functions as Objects in Python 2026

Writing Functions Mar 16, 2026

Functions as Objects in Python 2026

In Python, functions are first-class objects. This powerful feature allows you to treat functions like any other object — assign them to variables, pass them as arguments, return them from other functions, and store them in data structures. Understanding this concept is key to writing flexible and elegant code.

TL;DR — Key Takeaways 2026

  • Functions can be assigned to variables, passed as arguments, and returned from other functions
  • This enables powerful patterns like higher-order functions, callbacks, and decorators
  • Use function attributes and closures for advanced behavior
  • Functions as objects make code more modular and reusable

1. Basic Function as Object


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

# Functions are objects
say_hello = greet
print(say_hello("Alice"))        # Hello, Alice!

# Pass function as argument
def execute(func, arg):
    return func(arg)

print(execute(greet, "Bob"))     # Hello, Bob!

2. Advanced Patterns – Higher-Order Functions


# Return a function from another function
def make_multiplier(factor: int):
    def multiplier(x: int) -> int:
        return x * factor
    return multiplier

double = make_multiplier(2)
triple = make_multiplier(3)

print(double(10))   # 20
print(triple(10))   # 30

# Using as callback
def process_data(data, processor):
    return [processor(item) for item in data]

result = process_data([1, 2, 3, 4], lambda x: x * x)

3. Best Practices in 2026

  • Use functions as first-class objects to create flexible APIs
  • Pass functions as arguments for callbacks and strategy patterns
  • Return functions to create closures and factory patterns
  • Store functions in dictionaries for dynamic dispatch
  • Combine with type hints for better IDE support and clarity

What It Means That Functions Are Objects

In Python a function is an object of type function, just like a list or a string is an object. It has attributes such as __name__, __doc__ and __defaults__, it can be stored in variables and containers, passed to other functions and returned from them.

This is often described as functions being first-class citizens. It is the basis for callbacks, sorting with key=, decorators, plugin registries and many pandas methods such as apply and agg, which all accept functions as arguments.

Because a function is an object, you can also inspect it at runtime. The inspect module reads its signature, and attributes like __module__ tell you where it was defined, which is useful for logging and debugging.

You can even attach your own attributes to a function, although a class or a closure is usually clearer for storing state.

Worked Example: Inspecting and Using a Function Object

We read a function's attributes and pass it to built-in functions that expect a function.

import inspect

def net_price(price: float, tax: float = 0.17) -> float:
    """Price including tax."""
    return round(price * (1 + tax), 2)

print(type(net_price).__name__, net_price.__name__)
print(net_price.__doc__, net_price.__defaults__)
print(inspect.signature(net_price))

prices = [100, 40, 250]
print(list(map(net_price, prices)))
print(sorted(prices, key=net_price, reverse=True))

The object reports its type as function and its name as net_price, and shows the docstring and the default tax of 0.17. map applies it to every price, giving [117.0, 46.8, 292.5], and sorted uses it as a key.

Practical Tips

Use the inspect module when you build tools that call user-supplied functions. Checking the signature before calling gives a clear error message instead of a confusing TypeError deep in your code.

Remember that methods, built-in functions, classes and objects with a __call__ method are also callable. Code that accepts a function usually works with any of them, which keeps your helpers flexible.

Store function objects rather than their names as strings, so a typo is caught when the module loads.

Common Mistakes

  • Writing net_price() when passing the function, which passes its result instead.
  • Storing state in function attributes where a class would be clearer.
  • Comparing functions by name instead of by identity.
  • Forgetting that lambdas are function objects too, named <lambda>.

Quick Summary

  • Functions are objects with attributes.
  • They can be stored, passed and returned.
  • inspect.signature reads parameters.
  • Pass the function, not its result.

Conclusion

Treating functions as first-class objects is one of Python’s greatest strengths. In 2026, mastering this concept allows you to write more modular, reusable, and elegant code. From simple callbacks to complex decorator patterns, functions as objects unlock powerful design possibilities.

Next steps:

  • Review your codebase and look for opportunities to use functions as objects

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices