Inheritane syntax


#1

Hiya,
I know that the last two lines of this code is not required butt why do I get "" on the console?


Shape(object):
    """Makes shapes!"""
    def __init__(self, number_of_sides):
        self.number_of_sides = number_of_sides

# Add your Triangle class below!
class Triangle(Shape):
    def __init__(self, side1, side2, side3):
        self.side1= side1  
        self.side2=side2
        self.side3=side3
my_triangle = Triangle(6,3,7)
print my_triangle


#2

oops , excuse mygrammar ; should read "are not required"


#3

here:

Shape(object):

class keyword is missing.

if i run your code here, it prints triangle object as it should


#4

The "class" keyword is in my original code.I appear to be half asleep. I even forgot to type "On the console I get "" " The code is accepted by codecademy.


#5

something is wrong. with my typing. I meant to say that on the console it says


#6

<Triangle object at 0x7fa610917490>


#7

well, print my_triangle will print the object, which means python will tell you the object and its location in memory (<Triangle object at 0x7fa610917490>), how is this different from what you expected?


#8


Sorry


#9

to make <> show on the forum use format:

but that doesn't answer my question:


#10

I was expecting a print out of the sides of my triangle, i.e 6,3,7 somehow


#11

To make your class do what you expect, you would have to override the repr and return it.

def __repr__(self):
    return repr([self.side1, self.side2, self.side3])

Something like that. Currently it is returning what you are seeing. That is a very simplistic solution to what you was expecting to see. I'd suggest reading up on __repr__ and __str__ if you want to properly understand what it is all doing.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.