Question: bridge - demo.py Rectangle Class ( with comparison code ) In class, we created a very small product class with UPC code, description, and price.

bridge-demo.py
Rectangle Class (with comparison code)
In class, we created a very small product class with UPC code, description, and price. This code creates two products, then prints which has the smaller price.
The code illustrates the usage of __init__,__str__, and the comparison functions __eq__ and __gt__. There are six total comparison functions, but you only need to implement three (__eq__, either __gt__ or __lt__, either __ge__ or __le__).
class Product(object):
# UPC,Description,Price
def __init__(self,UPC,desc,price):
self.UPC = UPC
self.description = desc
self.price = price
def __str__(self):
s = self.UPC +","+ self.description +","
s += str(self.price)
return s
def __gt__(self,other):
return self.price > other.price
def __eq__(self,other):
return self.price == other.price
p1= Product("0000012345","Green Beans",1.49)
p2= Product("0000023456","1/2 Gallon Milk",3.79)
print(p1)
print(p2)
print(p1== p1)
print(p1== p2)
if (p1< p2):
print(p1.description,"is less expensive")
else:
print(p2.description,"is less expensive")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!