classes (xf ( hf (xh for small (yet moderatehsay - this estimate will be sufficiently accurate for most applications the key parts of the implementation are to let the function be an attribute of the derivative class and then implement the numerical differentiation formula in __call__ special methodclass derivativedef __init__(selffh= - )self self float(hdef __call__(selfx)fh self fself return ( ( +hf( ))/ make short forms the following interactive session demonstrates typical use of the classfrom math import df derivative(sinx pi df( - cos(xexact - def ( )return ** dg derivative(gt dg(tcompare with (exact for particularly useful application of the derivative classconsider the solution of nonlinear equation ( in we implement newton' method as general method for solving nonlinear equationsbut newton' method uses the derivative ( )which needs to be provided as an argument to the functiondef newton (fdfdxx max_it= tol - )return convergediter see for complete implementation of the function for many functions ( )finding (xcan require lengthy and boring derivationsand in such cases the derivative class is quite handydef ( )return *( )** ( )** dfdx derivative(fxstart newton (fdfdxxstart