TheAlgorithms/Python · error · ValueError
differentiate() requires a function as input for func
Error message
differentiate() requires a function as input for func
What it means
Raised by differentiate() in maths/dual_number_automatic_differentiation.py when the func argument is not callable. The routine drives automatic differentiation by evaluating func(Dual(position, 1)) and reading the resulting dual parts, so it must receive an actual function; anything else fails the callable(func) check with ValueError.
Source
Thrown at maths/dual_number_automatic_differentiation.py:119
>>> differentiate(lambda y: 0.5 * (y + 3) ** 6, 3.5, 4)
7605.0
>>> differentiate(lambda y: y ** 2, 4, 3)
0
>>> differentiate(8, 8, 8)
Traceback (most recent call last):
...
ValueError: differentiate() requires a function as input for func
>>> differentiate(lambda x: x **2, "", 1)
Traceback (most recent call last):
...
ValueError: differentiate() requires a float as input for position
>>> differentiate(lambda x: x**2, 3, "")
Traceback (most recent call last):
...
ValueError: differentiate() requires an int as input for order
"""
if not callable(func):
raise ValueError("differentiate() requires a function as input for func")
if not isinstance(position, (float, int)):
raise ValueError("differentiate() requires a float as input for position")
if not isinstance(order, int):
raise ValueError("differentiate() requires an int as input for order")
d = Dual(position, 1)
result = func(d)
if order == 0:
return result.real
return result.duals[order - 1] * factorial(order)
if __name__ == "__main__":
import doctest
doctest.testmod()
def f(y):
return y**2 * y**4
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a callable: differentiate(lambda x: x ** 2, 3.0, 1) or a named def.
- If you have a string expression, first convert it with eval in a controlled namespace or use a symbolic library — do not hand the raw string to differentiate().
- Drop the parentheses when passing named functions: f not f(x).
Example fix
# before
differentiate('x**2', 3.0, 1) # ValueError
# after
differentiate(lambda x: x ** 2, 3.0, 1) # 6.0 Defensive patterns
Strategy: type-guard
Validate before calling
if not callable(func):
raise TypeError(f'func must be callable, got {type(func).__name__}') Type guard
def is_func(f) -> bool:
return callable(f) Try / catch
try:
d = differentiate(func, position, order)
except ValueError as e:
if 'function as input' in str(e):
raise TypeError('pass a lambda or def, not an expression result') from e
raise Prevention
- Pass f, not f(x); pass lambda x: expr for inline expressions.
- Never hand raw expression strings to numeric routines — compile them first.
When it happens
Trigger: Calling differentiate('x**2', 3.0, 1), differentiate(42, 2.0, 1), or passing a result instead of a function like differentiate(x**2, 2.0, 1).
Common situations: Passing a string expression instead of a lambda; passing the already-computed value instead of the function object; passing an object whose class lacks __call__; confusion between a function reference f and a call f(x).
Related errors
- differentiate() requires a float as input for position
- differentiate() requires an int as input for order
- Both points must have the same dimension.
- Monogons and Digons are not polygons in the Euclidean space
- All values must be greater than 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/64e19f437fc8860a.
Report an issue: GitHub.