I am a newbie for python and try to understand class Inheritance.
Chris Rebert
clp2 at rebertia.com
Sat Oct 15 03:37:35 EDT 2011
On Sat, Oct 15, 2011 at 12:09 AM, Jason Swails <jason.swails at gmail.com> wrote:
<snip>
> For instance, let's say you want to deal with shapes. You can define a
> shape via a class
>
> class Shape(object):
> """ Base shape class """
<snip>
> Now we get into inheritance. Let's suppose that we want a specific type of
> shape. For instance, a circle (that is defined by an infinite number of
> vertices). In this case, a circle is still a shape, so it should have every
> attribute that a normal shape has. Thus, we can define a circle class as
> follows:
>
> class Circle(Shape):
> """ Circle inherits from Shape """
> number_vertex_points = 1000 # Default number of vertex points I want to
> define a circle
> def __init__(self, center, radius):
> """ Define the self.vertices here to trace out a circle as closely as
> you want """
>
> Now, each time we instantiate a Circle class, that class has every attribute
> that Shape has, in addition to any additional attribute you give to Circle
> (and if the same attribute is defined in both places, the definition in
> Circle overrides that definition). Thus, in this case we can define a
> Circle with a center and radius (much easier than vertices!), and we can
> tell Circle how we want the vertices defined to get as close an
> approximation to a circle as we want.
Sidenote: It's funny that the shapes example gets used so often,
despite the fact that pursuing it much further so easily leads to the
Circle-Ellipse / Rectangle-Square problem.
Cheers,
Chris
More information about the Python-list
mailing list