On Sat, Oct 15, 2011 at 2:20 AM, <aaabb...@hotmail.com> wrote: > Test.py > #!/usr/bin/python > from my_lib import my_function > class my_class(my_function.name): >
Classes must inherit from other classes -- not variables or functions. > def __initial__(self, name); > This should be "def __init__(self, name):" (: not ; and __init__, not __initial__) pass > def test(): > print "this is a test" > > If __name__ == '__maim__': > I think this should be '__main__' > my_class.main() > This only makes sense if your my_class has a main() attribute function to it. Note that the typical way of dealing with classes is to instantiate different classes. > > Can anyone finish above code and let me try to understand > Class inheritance? > You appear to be confusing functions and classes. Functions are chunks of code that you (optionally) pass variables to in order to perform a specific task. Classes, on the other hand, are the heart of OOP. They are useful for creating an abstraction layer to make certain problems easier and reading code easier as well. (It also helps break down the problem into more manageable pieces, which facilitates collaborative projects). 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 """ def __init__(self, vertices): """ Obviously if any of the vertices are collinear num_sides will be wrong. This is an example """ self.vertices = vertices self.num_sides = len(vertices) def draw(self, canvas): """ Draws the given shape on a passed canvas. Pretend there is working code here """ def area(self): """ Calculates the area of this shape and returns it as a floating point number """ return <area formula here> Now you can create different shapes as you need them user_shape_1 = Shape( ((0,0), (0,1), (1,1), (1,0)) ) user_shape_2 = Shape( ((-1,-1), (0,0), (-1,0)) ) Now you have 2 different shapes. If you have some kind of canvas object (from Tk, perhaps), you can draw either shape up there by calling one of its functions: user_shape_1.draw(my_canvas) As you can see, the simple call user_shape_1.draw(my_canvas) is self explanatory -- it draws the shape on the given canvas. This allows easy re-use of code. 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. HTH (I'm sure others can explain this better than I can), Jason
-- http://mail.python.org/mailman/listinfo/python-list