Exercises, examples and other material relating to training module Y116. This topic is presented on public courses Learning to program in Python
, Python Programming
, Intermediate Python
By this point on your course, you will have learned about the principles
of OO, and Object design, and also the features of the Python OO scripting
language. This module pulls the two threads together in order to show you
the practical application of the theory.
Related technical and longer articlesWhat makes a good variable name?
|Articles and tips on this subject||updated|
|4028||Really Simple Class and Inheritance example in Python|
It's so tempting when writing a simple training example to get excited and add lots of features - so that you then end up with an example that's anything but a simple training example!
Here's an example - just about my shortest and simplest ever - that shows a base class, a subclass, inheritance, polymorphism, ...
|3887||Inheritance, Composition and Associated objects - when to use which - Python example|
• Inheritance is where one object is based on another.
• Composition is where one object contains another.
Question: Which should I use?
Answer: Probably BOTH!
On yesterday's Python course, the question came up, and I wrote an illustrative answer - source code [here]. The scenario we ...
|2604||Tips for writing a test program (Ruby / Python / Java)|
Where does my test code go?
If you've written a class - a series of methods to be used within another application - how do you test it? How about writing a test program within the same file which runs as the main program if you run your class on its own from the command line, but is ignored if you ...
|2523||Plan your application before you start|
Planning your project / application ahead of time can save you so much heartache and wasted time that it's untrue - but there is still a tendency for people to jump right into to coding without thinking "how will this work". And you should think "How will this work" in many ways.
These seven diagram ...
|2485||How do I set up a constant in Python?|
You may think of values like pi being a constant, but in Python they're really just another object that's defined in the namespace of the module from which they're loaded!
>>> import math
>>> math.pi *= 4
You are fully entitled to ...
|2407||Testing code in Python - doctest, unittest and others|
The doctest and unittest modules of Python allow you to provide test harnesses for your classes / packages. Designing applications from the bottom up, you'll want to ensure that each of your code levels works and works well. You'll want to provide an example of what it should do for the next level ...
|2363||Alpaca Case or Camel Case|
A very first practical program on our Learning to Program in Python course that started today.
Even at this early stage, we teach things like good practise in variable naming - standards such as using a consistent case (e.g. camel case where each intermediate words starts with a capital).
I was looking ...
|1181||Good Programming practise - where to initialise variables|
It's a good idea to initialise your variables directly before you use them for the first time if you're going to use them as accumulators.
By accumulator I mean that you're going to write assignments such as:
$n += 4; # Perl
incr notepad; # Tcl
lappend flcodes [lindex ...
|945||Code quality counts|
We're celebrating Geekmas this weekend ... and we've got a full place. Plus others who are coming in for the day. Really good session yesterday on the good DESIGN of an interactive (scripted) web site that "mines" user information to give a tailored presentation, and a great intro by Paddy to Python's ...
|836||Build on what you already have with OO|
If I start recalling a "Goons" sketch, then I'll show my age .... but never mind. It was a RERUN that I saw ;-)
Spike Milligan is counting votes at an election. "Two thousand, six hundred and forty two". "Two thousand, six hundred and forty three". "Two thousand, six hundred and forty four" ...
|668||Python - block insets help with documentation|
For many programmers, documenting their code is a chore, yet every piece of code that's going to be (re)used needs both instructions for the user, and adequate explanation of techniques for the code maintainer. The need for BOTH of these forms of support documentation is often overlooked by the newcomer, ...
Examples from our training material
|constant.py|| Constants in Python|
|ddd.py|| Good practise tips and documentation string|
|novel|| Traffic survey - Python and Objects|
|sob|| Data for novel and story examples|
|story|| Traffic survey - Python without Objects|
|t6|| inherited and associated / composite objects|
|t7|| completion of example t6 - add factory, comparator, etc |
This module is available under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
Review of OO design techniques.
Review of OO facilities in Python.
Naming conventions in Python.
How to arrange your classes, modules and packages.
Documentation and code control.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.