Getting Started with Python
Written by Mike James   
Thursday, 01 March 2018
Article Index
Getting Started with Python
Control Statements

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 i.e. explicitly setting the self parameter.

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):
   return self.ans

Now you can write:


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:

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):

in the point definition you can now initialise a point using


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.

There is a lot more to find out about object oriented Python and this is covered in later chapters.


When you write a program 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__”:

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.

Using predefined modules is one of the strong points of Python and you will find that using import is one of the quickest ways to build a program.

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 reason is that if you have used almost any other language then you will have expectations of some sort of framework to enable you to create a user interface. Python doesn't have a built in standard approach to the problem like languages such as C# and Java. It has a lot of different frameworks that do the job in many different ways  - more than 30 are listed on the Python web site.

The Python standard way of doing this is to make use of Tkinter, which is a Python interface to the standard GUI toolkit Tk. It is standard in the sense that it is usually automatically installed and ready for you to use.

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 *
Button(root,text="Hello World",command=exit).pack()

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.

Using other frameworks under Python is very similar to this. 

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 of course this is the purpose of the rest of this book.

Programmer's Python
Everything is an Object
Second Edition

Is now available as a print book: Amazon



  1. Get Ready For The Python Difference
  2. Variables, Objects and Attributes
  3. The Function Object
  4. Scope, Lifetime and Closure
  5. Advanced Functions
  6. Decorators
  7. Class, Methods and Constructors
      Extract 1: Objects Become Classes ***NEW!
  8. Inside Class
  9. Meeting Metaclasses
  10. Advanced Attributes
  11. Custom Attribute Access
  12. Single Inheritance
  13. Multiple Inheritance
  14. Class and Type
  15. Type Annotation
  16. Operator Overloading
  17. Python In Visual Studio Code

 Extracts from the first edition

Advanced Attributes




or email your comment to:


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.








Last Updated ( Friday, 13 November 2020 )