Python 3x class methods -
i'm learning basics of classes, , came basic code follows:
class shape(object): def __init__(self, num_sides, type): self.num_sides = num_sides self.type = type class square(shape): def __init__(self, side_length): self.num_sides = 4 self.type = 'regular quadrilateral' self.side_length = side_length def perim(): return side_length * 4 def area(): return side_length ** 2 class circle(shape): def __init__(self, radius): self.num_sides = 1 self.type = 'ellipsis' self.radius = radius
now, when type following:
shape1 = square(5) shape1.perim()
i following output:
<bound method square.perim of <__main__.square object @ 0x0000000003d5fb38>>
what this? how can python return perimeter of square?
also, have question: class methods exist other __init__()
, __str__()
? if so, can please list them can go off , research them?
as shown, going have problems this. if trying have circle , square subset set classes of shape, 2 sub classes need indented. also, on class circle , square, not need (shape). note things commented out not needed.
this not come out posting it. class shape (object):does not show , subclasses not indented , can not seem make show up
class shape(object): def init(self, num_sides, type): #self.num_sides = num_sides self.type = type
class square: def __init__(self, side_length): #self.num_sides = 4 #self.type = 'regular quadrilateral' self.side_length = side_length def perim(self): return self.side_length * 4 def area(self): return self.side_length ** 2 class circle: def __init__(self, radius): #self.num_sides = 1 #self.type = 'ellipsis' self.radius = radius def area (self): return 3.14 * self.radius ** 2 shape2 = circle (5) print ('test of circle: ',shape2.area ()) shape1 = square(5) print('test of square: ', shape1.perim())
Comments
Post a Comment