-1

I'm a newbie and have hit a brick wall when trying to call back and display the data of this list. Instead I get only object IDs.

<__main__.Square object at 0x000001FBDF9C7040>
<__main__.Circle object at 0x000001FBDF9C6F80>
<__main__.Cuboid object at 0x000001FBDF9C6E90>
class Cuboid:
    def __init__(self, length=4.0, width=3.0, height=2.0):`
        self.length = length
        self.width = width
        self.height = height
    def setLength(self, length):
        self.length = length
    def setWidth(self, width):
        self.width = width
    def setHeight(self,height):
        self.height = height
    def calArea(self):
        return (2 * self.length * self.width) + (2 * self.length * self.height) + (2 * self.height * self.width)
    def display(self):
        print("With length", self.length, "width", self.width, "and height", self.height)
        print("the area of the cuboid is", self.calArea())
        print("\n")

class Circle:
    def __init__(self, radius=4.3):
        self.radius = radius
    def setRadius(self, radius):
        self.radius = radius
    def calArea(self):
        return self.radius * 2 * math.pi
    def display(self):
        print("With a radius of " + str(self.radius))
        print("the area is ", self.calArea())
        print("\n")

class Square:
    def __init__(self, length=4.0):
        self.length = length
    def setLength(self, length):
        self.length = length
    def calArea(self):
        return self.length * self.length
    def display(self):
        print("With length of sides being " + str(self.length))
        print("the area of the square is", self.calArea())
        print("\n")

def randomizer():
    x = 10
    while x > 0:
        pick = random.randint(0,2)
        print(pick, "was randomly selected.")
        if pick == 0:
            circleObj = Circle()
            ranlist.append(circleObj)
            circleObj.display()
        if pick == 1:
            squareObj = Square()
            ranlist.append(squareObj)
            squareObj.display()
        if pick == 2:    
            cuboidObj = Cuboid()
            ranlist.append(cuboidObj)
            cuboidObj.display()
        x -= 1

def main():
    randomizer()
    for x in range(0, len(ranlist)):
        print (str(ranlist[x]))

main()

Googling has led me to try declaring str() and using __str__ and __repr__ but I feel like I just keep breaking it, assuming I am either implementing it incorrectly, or perhaps the problem is with my class structure to begin with. I can print display() just fine on its own, but when printing from the list gives only object IDs.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Where have you defined str and repr? – nokla Dec 09 '22 at 23:16
  • Use `ranlist[x].display()` – Barmar Dec 09 '22 at 23:18
  • What would you like the output to look like? For each of these classes, implement a `def __str__(self):` that does that. There is also a `__repr__` method for human visible display. str is pretty, repr is technical (usually including a hint on what the object type is). When printing a list, python will use each value's `__repr__`. If you want its prettier form, you would `print(", ".join(val) for val in lst)`. – tdelaney Dec 09 '22 at 23:18

3 Answers3

0

Understanding you want to print properties of an object for every object in your list, you can get all the properties of a given object as a dictionary with vars(your_object) and prettyprint it.

See Is there a built-in function to print all the current properties and values of an object? for reference. To prevent deadlink, top answer was:

from pprint import pprint
pprint(vars(your_object))
3yakuya
  • 2,622
  • 4
  • 25
  • 40
0

Replace:

print (str(ranlist[x]))

with:

ranlist[x].display()

Or define __str__() methods in your classes that return the same strings that the display() methods currently print. E.g.

class Square:
    def __init__(self, length=4.0):
        self.length = length

    def setLength(self, length):
        self.length = length

    def calArea(self):
        return self.length * self.length

    def __str__(self):
        return f"""With length of sides being {self.length)}
the area of the square is {self.calArea()}"""

    def display(self):
        print(str(self))
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can define a base class doing the describing job and let objects derive functionality from:

from numpy import random
import math


class DisplayableBase:
    def __str__(self):
        return self.describe()
    def describe(self):
        return "(undefined)"
    def display(self):
        print(str(self))
    
class Cuboid(DisplayableBase):
    def __init__(self, length=4.0, width=3.0, height=2.0):
        self.length = length
        self.width = width
        self.height = height
    def setLength(self, length):
        self.length = length
    def setWidth(self, width):
        self.width = width
    def setHeight(self, height):
        self.height = height
    def calArea(self):
        return ((self.length * self.width) + 
                (self.length * self.height) + 
                (self.height * self.width)) * 2
    def describe(self):
        return (f"With length {self.length}, "
                f"width {self.width}, and height {self.height}\n"
                f"the area of the cuboid is {self.calArea()}\n")

class Circle(Displayable):
    def __init__(self, radius=4.3):
        self.radius = radius
    def setRadius(self, radius):
        self.radius = radius
    def calArea(self):
        return self.radius**2 * math.pi
    def describe(self):
        return f"With a radius of {self.radius}, the area is {self.calArea()}\n"

class Square(Displayable):
    def __init__(self, length=4.0):
        self.length = length
    def setLength(self, length):
        self.length = length
    def calArea(self):
        return self.length * self.length
    def describe(self):
        return (f"With length of sides being {self.length}, the "
               f"area of the square is {self.calArea()}\n")

def randomizer():
    x = 10
    ranlist = []
    while x > 0:
        pick = random.randint(3)
        print(pick, "was randomly selected.")
        if pick == 0:
            obj = Circle()
        elif pick == 1:
            obj = Square()
        elif pick == 2:    
            obj = Cuboid()
        ranlist.append(obj)
        obj.display()
        x -= 1
    return ranlist

def main():
    ranlist = randomizer()
    for x in ranlist:
        print(str(x))
        #print(x.describe())
        #x.display()

main()

(I corrected some other errors, too)

Andreas
  • 159
  • 1
  • 7