<div class="gmail_quote">On Tue, Oct 27, 2009 at 10:20 AM, John <span dir="ltr"><<a href="mailto:jfabiani@yolo.com">jfabiani@yolo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"><br>
</div></div>Hey guru's could one of you explain why such a subclass is needed. How would<br>
it be used. I'm not sure but does not the deepcopy() work as above?</blockquote><div><br></div><div>A subclass is useful for when you want to do pretty much what another class does, only with some modifications.</div>
<div><br></div><div>My favorite example (and the easiest to understand) deals with shapes:</div><div><br></div><div>class Shape:</div><div> def __init__(self):</div><div> self.sides = 0</div><div> self.area = 0</div>
<div><br></div><div>class Triangle(Shape):</div><div> Shape.__init__(self)</div><div> def __init__(self, base=0, height=0):</div><div> self.sides = 3</div><div> self.area = base/2*height</div><div>
<br></div><div>class Circle(Shape):</div><div> Shape.__init__(self)</div><div> def __init__(self, radius):</div><div> self.sides = 1</div><div> self.area = 2*3.14*radius</div><div><br></div><div>
<br></div><div>There are many other times when subclasses are useful, but that's a nice simple example. The "is-a" relationship can tell you when it's useful to have a superclass. "This collection is a list of (un)ordered elements" would tell you it should be a list. Though, TBH most python objects are so "batteries" included that I have little (no) reason to subclass - my only real exception is GUI programming.</div>
<div><br></div><div>Here's something Google pulled up on the subject: <a href="http://www.gidnetwork.com/b-137.html">http://www.gidnetwork.com/b-137.html</a></div><div><br></div><div>HTH,</div><div>Wayne</div><div><br>
</div></div>