Wojciech Moszczyński
Wojciech Moszczyński

Reputation: 3187

Direct printing in class - python

I am new in 'class' methods, so sorry if somebody feel resentful. Every many people know this example:

class Rectangle:
    def __init__(self, length, breadth, unit_cost=0):
        self.length = length
        self.breadth = breadth
        self.unit_cost = unit_cost

    def get_perimeter(self):
        return 2 * (self.length + self.breadth)

    def get_area(self):
        return self.length * self.breadth

    def calculate_cost(self):
        area = self.get_area()
        return area * self.unit_cost

Now to get information we need to do such operation:

r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s cm^2" % (r.get_area()))
print("Cost of rectangular field: Rs. %s " %(r.calculate_cost()))

Result:

But I dont do it this I need write this:

Rectangle(160, 120, 2000)

and get answer right now:

I can use ordinary def myfunction: but I would like to do it by class. Thanks for every help!

Upvotes: 1

Views: 136

Answers (2)

Derek O
Derek O

Reputation: 19565

Use __repr__ to make a printable representation of your class, then add a print statement from within __init__ if you want to avoid using print outside of the class (you can make this optional).

class Rectangle:
    def __init__(self, length, breadth, unit_cost=0):
        self.length = length
        self.breadth = breadth
        self.unit_cost = unit_cost
        print(self)

    def __repr__(self):
        area_str = "Area of Rectangle: %s cm^2" % (self.get_area())
        cost_str = "Cost of rectangular field: Rs. %s " %(self.calculate_cost())
        return area_str + "\n" + cost_str

    def get_perimeter(self):
        return 2 * (self.length + self.breadth)

    def get_area(self):
        return self.length * self.breadth

    def calculate_cost(self):
        area = self.get_area()
        return area * self.unit_cost

Output:

r = Rectangle(160, 120, 2000)

Area of Rectangle: 19200 cm^2
Cost of rectangular field: Rs. 38400000 

Upvotes: 1

jfaccioni
jfaccioni

Reputation: 7519

If you implement the code you wrote in the print statements as the class __str__ method, you'll get that result by printing the class itself:

class Rectangle:
    def __init__(self, length, breadth, unit_cost=0):
        self.length = length
        self.breadth = breadth
        self.unit_cost = unit_cost

    def __str__(self):
        return "Area of Rectangle: %s cm^2\nCost of rectangular field: Rs. %s " % (self.get_area(), self.calculate_cost())

    def get_perimeter(self):
        return 2 * (self.length + self.breadth)

    def get_area(self):
        return self.length * self.breadth

    def calculate_cost(self):
        area = self.get_area()
        return area * self.unit_cost

And the output:

>>> print(Rectangle(160, 120, 2000))                                                                                                                        
Area of Rectangle: 19200 cm^2
Cost of rectangular field: Rs. 38400000

This is a better design decision than adding a print statement to __init__, since it's relatively painless to add a print statement around the Rectangle() call, and it's more flexbile because you can still choose whether you want to display the output or not.

Upvotes: 4

Related Questions