Python Fundamentals: Implementing a subtraction of a square class

Source: Internet
Author: User

1 classSquare:2     def __init__(Self, WH):#because it is a square, it takes only one side of the length3         ifisinstance (WH, (int,float)):4SELF.WH =WH5         Else:6             RaiseTypeError7 8     def __add__(self, Other):9Self_area = SELF.WH * SELF.WH#Calculate self AreaTenOther_area = OTHER.WH * OTHER.WH#Calculate other Area One         returnSelf_area + Other_area#Add X+y A  -     def __sub__(self,other): -Self_area = SELF.WH * SELF.WH#Calculate self Area theOther_area = OTHER.WH * OTHER.WH#Calculate other Area -         returnSelf_area-other_area#subtract x-y -      -     def __mul__(self,other): +Self_area = SELF.WH * SELF.WH#Calculate self Area -Other_area = OTHER.WH * OTHER.WH#Calculate other Area +         returnSelf_area * Other_area#Multiply X*y A      at     def __mod__(self,other): -Self_area = SELF.WH * SELF.WH#Calculate self Area -Other_area = OTHER.WH * OTHER.WH#Calculate other Area -         returnSelf_area% Other_area#Divide x%y -  -S1 = Square (3) inS2 = Square (4)

Python Fundamentals: Implementing a subtraction of a square class

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.