This example describes how Python implements the reciprocal calculation. Share to everyone for your reference. Specific as follows:
class expr:def __add__ (self, Other): Return Plus (Self, other) def __mul__ (self, Other): Retu RN times (self, Other) class Int (Expr): Def __init__ (self, n): SELF.N = n def d (self, V): return Int (0) def __str__ ( Self): Return ' SELF.N ' class Var (Expr): Def __init__ (Self, var): Self.var = var def d (Self, V): Return Int (Self.va r = = V and 1 or 0) def __str__ (self): Return Self.var class Plus (Expr): Def __init__ (self, A, b): Self.e1 = a self . E2 = b def d (self, V): Return Plus (self.e1.d (v), self.e2.d (v)) def __str__ (self): return "(%s +%s)"% (Self.e1, SE LF.E2) class times (Expr): Def __init__ (self, A, b): Self.e1 = a self.e2 = b def d (self, V): Return Plus (the . E1, Self.e2.d (v)), Times (self.e1.d (v), self.e2)) def __str__ (self): return "(%s *%s)"% (Self.e1, self.e2) if __name__ = = "__main__": x = var ("x") A = var ("a") B = var ("b") C = var ("c") e = A * x * x + b * x + c print "D (%s, x) =%s" % (E, E.D ("x"))
Hopefully this article will help you with Python programming.