Closed robraddi closed 6 years ago
I would alter this analogy in the following way:
#!/usr/bin/env python
#import triangle
import numpy as np
### BTW, you can define many classes in the same file! ###
class Shape:
'''
A parent class for all shapes.
'''
def __init__(self, nSides, nVertices,Name):
self.nSides = nSides
self.nVertices = nVertices
self.Name = Name
class Triangle(Shape):
'''A derived class for a triangle.'''
def __init__(self, nSides=3, nVertices=3, Name='Triangle', color='red'):
self.nSides = nSides
self.nVertices = nVertices
self.Name = Name
self.color = color
def draw(self, height='0.4', width='0.3'):
'''Draw the shape.'''
print 'Name:', self.Name
print 'Sides:', self.nSides
print 'Vertices:', self.nVertices
print 'Color:', self.color
print 'Height:', height
print 'Width:', width
# Main program
# let's draw a patriotic picture
my_shapes = []
my_shapes.append( Triangle() )
my_shapes.append( Triangle(color='white') )
my_shapes.append( Triangle(color='blue') )
# now we render the picture -- happy 4th!
for s in my_shapes:
s.draw()
Yay it works!
CST11352:Desktop suvince$ python shapes.py
Name: Triangle
Sides: 3
Vertices: 3
Color: red
Height: 0.4
Width: 0.3
Name: Triangle
Sides: 3
Vertices: 3
Color: white
Height: 0.4
Width: 0.3
Name: Triangle
Sides: 3
Vertices: 3
Color: blue
Height: 0.4
Width: 0.3
p.s. let's talk about this more at our meeting -- there are other ways we can improve this too
This is understood. I always appreciate your help, Vince.