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.