Python implementation of the reciprocal calculation method

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.