[Tutor] How to load a dict into a dict subclass?
Wayne
srilyk at gmail.com
Tue Oct 27 16:44:47 CET 2009
On Tue, Oct 27, 2009 at 10:20 AM, John <jfabiani at yolo.com> wrote:
>
> Hey guru's could one of you explain why such a subclass is needed. How
> would
> it be used. I'm not sure but does not the deepcopy() work as above?
A subclass is useful for when you want to do pretty much what another class
does, only with some modifications.
My favorite example (and the easiest to understand) deals with shapes:
class Shape:
def __init__(self):
self.sides = 0
self.area = 0
class Triangle(Shape):
Shape.__init__(self)
def __init__(self, base=0, height=0):
self.sides = 3
self.area = base/2*height
class Circle(Shape):
Shape.__init__(self)
def __init__(self, radius):
self.sides = 1
self.area = 2*3.14*radius
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.
Here's something Google pulled up on the subject:
http://www.gidnetwork.com/b-137.html
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091027/8ced801a/attachment-0001.htm>
More information about the Tutor
mailing list