|
Page 4 of 4
Inheriting the point
So what about inheritance? Surely an interpreted system can’t handle inheritance at all well?
Python can and it is all done interactively. If you use a class definition of the form:
class name(superclass):
then the new class inherits all of the methods defined in superclass.
For example,
class complexpoint(point)
defines a new class called complexpoint and inherits all of the methods defined in point.
You can, of course, define new methods and override existing methods.
If a method cannot be found in the class’s definition the superclass definition is searched. If the method cannot be found in the superclass then any super-superclass that might exist is searched and so on until a definition is found or an error is generated.
You can also arrange to call the inherited method directly using the class name as the first parameter of the call. In fact all method calls are translated from:
instance.method(parameters)
to
class.method(instance,parameters)
and the first parameter is the self you include in every method definition. .
You can call instance methods directly using this form. For example,
point.setpos(x,1,2)
is entirely equivalent to
x.setpos(1,2)
and of course this is the trick Python uses to implement object instances.
Finally, it is worth knowing that operator overloading is supported using a range of standard method names.
For example, if you want to define an addition for points then you need the following method:
def __add__(self,other): self.ans=(self.pos[0]+other.pos[0], self.pos[1]+other.pos[1]) return self.ans
Now you can write:
z=x+y
where x and y are point objects. Notice that z isn’t a point object, but a tuple and this can be a problem if you want to use the overloaded plus operator a second time. In general, operators should return results of the same type.
If you want to return a point object you have to use:
ans=point()
ans.setpos(self.pos[0]+other.pos[0], self.pos[1]+other.pos[1]) return ans
Overriding operators might not seem like something you want to do, but after using Python for a while I can assure you that you will – if only to write constructors.
The __init__ operator is the objects constructor and if you over right it you can define a non-default constructor. For example, if you include:
def __init(self,x=0,y=0): self.position=(x,y
in the point definition you can now initialise a point using
x=point(1,2)
and if you don’t specify a value then a (0,0) point is constructed by default. There is also a destructor operator, indexing operator, slicing operator and so on, all of which can be overloaded.
Modules and files
So far all of the Python code that we have used as examples has been entered using the interactive window.
While this is a good way of finding out how Python works it is no way to build a real program.
A much better way is to create a script file. Simply use the File,New Window command and type in your Python commands.
Then save the file with an appropriate name and run it using the Run Module command or press F5.
Any commands in the script are obeyed and any output goes to the Shell window that you have been using to try out Python.
Although it isn’t necessary it is fairly common to give the script a “main function” type structure using the single line:
if __name__== “__main__”: main()
When Python runs a module it sets __name__ to main and this can be used to select the function to start the script off. Scripts can be stored and used as modules within other scripts.
To load a script as a module you use the command:
import modulename
In this case the script is loaded but __name__ isn’t set to “__main__” and this can be used to suppress the startup routine in the script.
If you only want to import a subset of the names in a module you can use the:
from module import name
command instead and make use of wildcard characters to express patterns that the name has to match.
Where is the GUI?
At this point we could look at any of the many specialised facilities that Python offers the programmer, but we cannot leave the subject of writing scripts without a brief discussion of creating a platform-independent GUI.
The standard way of doing this is to make use of Tkinter, which is a Python interface to the standard GUI toolkit Tk.
The advantage of using Tkinter is that is powerful and platform-independent but it can be slow and needs yet another package to be installed. The subject of using Tkinter is too large a one to explain in detail but a simple “Hello World” example will show you what is involved. Open a new window and type in the following:
from sys import exit from Tkinter import * root=Tk() Button(root,text="Hello World", command=exit).pack() root.mainloop()
If you run this module the result will be a small button labelled “Hello World”.
You can see how the program works – create an instance of Tk, create a button and then run the event handling loop.
Now you know how to create a button the rest is just a matter of creating more controls and writing event handlers.
Where next
This lightning tour of Python has attempted to give you the flavour of the language and point out the features that confuse the programmer used to other object oriented languages like C or Java. Now you simply have to build on this foundation by writing some programs. Python has some very good documentation to help you and there are some good books on the subject, see side panel.
Online resources you might like are:
<ASIN:1435455002>
<ASIN:0672329786>
<ASIN:1590599829>
|