Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Python module Y106
Object Oriented Python
Exercises, examples and other material relating to training module Y106. This topic is presented on public courses Learning to program in Python
, Python Programming
, Intermediate Python
Python is an object oriented language. You can group code that relates to a particular type of data together into a class, with each piece of code that's defined being a method that can run on the class as a whole, or on a specific set of data (object).
Related technical and longer articlesall variables are objectsPython Calling functions and methods. Using objects and modules.copying an object - copy the reference
|Articles and tips on this subject||updated|
|4721||When to check an object type - Python isinstance example|
I'll encourage you not to look at an object in a Python program - or in any other OO langauge - you find out what type it is ... at least until you've thought about why you're doing it.
If you need to, you can look - you have the type function, you have the __class__.__name__ variable and you have isinstance.
|4650||Why populate object with values as you construct them?|
Objects are a great way to bundle up a whole lot of values (also known as properties or attributes) into a single container / collection, allowing the the programmer who makes use of the objects to be able to set them up just once and delegate the management of the values to the class / object code. ...
|4591||From single block to structure and object oriented programming|
If you're writing a very short, one-off program, you're likely to mix your calcualtion details in with your data input / output and data format handling - "quick and easy" - for example, code (Python 2.7 and 3.x) [here].
But as your application grows, and the code grows, a single block becomes less ...
|4448||What is the difference between a function and a method?|
I strongly encourage delagates on our courses to divide their code into managable, understandable, testable, re-usable chunks rather than write it all into a single block. And the adjectives I've used (managable, understandable, testable, re-usable) explain why.
You can process data within these chunks ...
|2169||When should I use OO techniques?|
We have two friends. They're married. She's a keen advocate of Object Oriented Programming and he can't stand the approach. So take them to the pub, buy them a couple of drinks each, mention OO programming and watch the feathers fly ;-)
Seriously, Object Oriented Programming has its place - and there ...
|4129||Simple OO demonstration in C++, comparison to Python|
From last week's quick introduction to C++ (a private course - half a day added on to the end of a C Programming course - an example of how C++ implements objects for a newcomer to C++ who's seen some Python.
Firstly, in C++ you must predefine the API - the interface between the user and the class. ...
|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, ...
|4021||Spike solution, refactored and reusable, Python - Example|
The other day, we suffered a gap in service from one of our dedicated hosts and after the event I wanted to go through the server logs to find how long the gap was, and whether there was any unusual activity prior to the outage. Problem was - I was looking in a 60 Mbyte log file with ovr 200,000 records ...
|3947||this or self - what are they, and what is the difference? (Python) |
In any object oriented language, you'll have a number of different objects of the same type, and you'll need to be able to refer to a paricular attribute within a particular object - usually, you'll call a named piece of code (a method) on an object and need to say "on THIS object" ... let me rephrase ...
|3878|| From Structured to Object Oriented Programming.|
A frequent comment - "I'm very used to conventional / structured programming, but I'm now trying to learn about Object Orientation and I don't understand the idea and I get baffled by all the buzzwords I hear". And a request "Can you help me?"
Yes - I can help you. If you come on a course ...
|3673||Object oriented or structured - a comparison in Python. Also writing clean regular expressions|
Should I go "object oriented" with my code, or use functional coding? That's a big question, and the answer depends on the application you'll be writing, the language you'll be writing it in, the programmers concerned, the expected life cycle of the application, and whether you'll be able to reuse ...
|3436||Moving from scripting to Object Orientation in Python|
Using what's been described as the scripting idiom, a "scripter" will write a series of code steps which perform a task from top to bottom. For a job that only involves simple data manipulation (even if there's a lot of data to have that manipulation applied), this approach can be very effective indeed ...
|3399||From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)|
Money's useful stuff.
Before money, transactions were undertake on a barter system; if I wanted a loaf of bread I might exchange it for a fish. If I was wanting to book passage on tthe stagecoach from the George in Melksham to London, I would have to take ia bushel of apples into my local travel agent, ...
|477||Class, static and unbound variables|
In Object Oriented programming, you'll have certain named blocks of code (usually known as methods) that you can perform on specific objects (type A), and others that you can perform on all the objects of a particular type (type B).
If I had a class called sandwich, I might have one method ...
|3085||Object Oriented Programming for Structured Programmers - conversion training|
I've been programming for many years; I started off with languages like Fortran and quickly learned that it's a good idea to split the code into named chunks - which were known as FUNCTIONs and SUBROUTINEs. This means that parts of a program can be tested named chunk by named chunk, that chunks can ...
|2017||Python - a truly dynamic language|
In Python everything is an object - and that includes functions which are objects which contain blocks of code. And this means that you can define different functions of the same name depending on a condition, and you can replace a function with another one too, if you want to.
This really is valid:
|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 ...
|1925||Introduction to Object Oriented Programming|
"Object Oriented Programming" is a whole new philosophy for programmers who have been writing short scripts for years, and it can be quite frightening to learn with all these new buzzwords like "overriding", "encapsulation" and "polymorphism" creeping in. It's a beautiful concept and a lovely approach, ...
|1348||Screw it or Glue it? Access to Object variables - a warning|
If you screw two pieces of material together, you have the opportunity, later on, to unscrew them ... resuse them differently, install a waterproof membrane between them, and so on. But if you glue them together, later modification will be much harder or perhaps impossible. And in a similar way, if ...
|1306||Python class rattling around|
"Occupany Limit 230" says the room that I'm running my Python training course - for 8 delegates - in at the Watervliet Arsenal near Albany, New York State this week. Watervliet is being "demilitarised", which means that there's a whole lot of commercial companies - including the one I'm teaching - based ...
|900||Python - function v method|
What's the difference between a function and a method? A function is a named piece of code that performs an operation, and a method is a function with an extra parameter which is the object that it's to run on.
self.name = ...
|834||Python makes University Challenge|
"In computer programming terms, what does OOP signify when used in the context of languages such as Python and Java" .... I couldn't quite believe my ears when I heard Jeremy Paxman asking that question last night on University Challenge. ...
Examples from our training material
|animal.py|| Sample class (exercise) - animal|
|aper2|| retaining data objects for iteration|
|c1|| Defining and using a class - early steps|
|containers.py|| Shapes used for items in junkroom|
|creatures|| Exercise answer - program to test class animal|
|cubbyhole|| Simple class - with a class (static) variable|
|dirStream.py|| Object using generator - directory file traverser|
|drink.py|| definition and testing of a class|
|forum.py|| Class of records - forum log file analysis|
|glue|| Shows danger of using direct access to a member variable|
|longer|| Saving a lambda in a variable and other tricks|
|mute|| mutable v immutable objects|
|pets|| OO sample - definition and use of two dog objects|
|pyobs|| test program for containers in junkroom|
|rr2|| Object equality, identity and sameness|
|survey.py|| Work in progress - survey analysis|
|tabby.py|| table, round table and rectangular table|
|typelook|| looking at an object type|
|webhit.py|| Web Server access - directory report|
|wr2.py|| Test application for "writer" class|
|wr_test.py|| Test harness for writer class|
|writer.py|| A simple class - constructor, object method and class method, no inheritance|
PicturesClasses in Python
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
What is Object Orientation and why use it?.
Using Python objects.
Creating your own classes in Python.
Method and variable naming standards.
Including a test harness in your class definition.
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.