Demo entry 6677176

Pythonic

   

Submitted by anonymous on Dec 02, 2017 at 20:44
Language: Python. Code size: 2.6 kB.

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

r1 = Rectangle(3,6)
r2 = Rectangle(3,5)

print r1 > r2
print r1 < r2
print r1 >= r2
print r1 <= r2
print r1 == r2


class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def isBiggerThan(self, other):
        return (self.height*self.width) > (other.height*other.width)

    def isSmallerThan(self, other):
        return (self.height*self.width) <= (other.height*other.width)

    ...


class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __eq__(self, other):
        return (self.height*self.width) == (other.height*other.width)

    def __lt__(self, other):
        return (self.height*self.width) < (other.height*other.width)

    def __gt__(self, other):
        return (self.height*self.width) > (other.height*other.width)

    def __ge__(self, other):
        return (self.height*self.width) >= (other.height*other.width)

    def __le__(self, other):
        return (self.height*self.width) <= (other.height*other.width)

r1 = Rectangle(3,6)
r2 = Rectangle(3,5)

print r1 > r2
print r1 < r2
print r1 >= r2
print r1 <= r2
print r1 == r2

print "...."
class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __eq__(self, other):
        return (self.height*self.width) == (other.height*other.width)

    def __lt__(self, other):
        return (self.height*self.width) < (other.height*other.width)

    def __gt__(self, other):
        return (self.height*self.width) > (other.height*other.width)

    def __ge__(self, other):
        return (self.height*self.width) >= (other.height*other.width)

    def __le__(self, other):
        return (self.height*self.width) <= (other.height*other.width)

r1 = Rectangle(3,6)
r2 = Rectangle(3,5)

print r1 > r2
print r1 < r2
print r1 >= r2
print r1 <= r2
print r1 == r2

print "...."
from functools import total_ordering

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __eq__(self, other):
        return (self.height*self.width) == (other.height*other.width)

    def __lt__(self, other):
        return (self.height*self.width) < (other.height*other.width)

r1 = Rectangle(3,6)
r2 = Rectangle(3,5)

print r1 > r2
print r1 < r2
print r1 >= r2
print r1 <= r2
print r1 == r2

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).