Foundations: The Ministry of Silly Objects

DENNIS: What I object to is that you automatically treat me like an inferior!
ARTHUR: Well, I am king!
(Scene 3)

This post is about “objects”.   Not everyday sort of objects that you can touch.  Programming objects.  In fact, not any old programming objects, but Python objects.   Objects are a way of packaging together data and ways of dealing with the data.   In the previous tutorial we encountered a variable called fileObject – from last tute:

... fileObject = open(nameOfFile,'w')
>>> # fileObject is a file object
... # the open() function creates a file
... # and then
... # it opens the file for writing - 'w'
... # if the file already exists it just opens it

>>> # see, now it exists
>>> # let's write something to it:
... testMessage='This is a test message'
>>>
>>> fileObject.write(testMessage)
>>> # after we've finished with a file we should close() it ... fileObject.close()

The variable fileObject stores an object in much the same way as other variables store data – that is, they point to the object.  There is nothing special about the name, I just used it to call out the fact that an object was being stored in it.    What I want to draw attention to is that we created fileObject by calling a function – open().  However, we didn’t close it by calling another function close().   Rather, we closed it another way:

... fileObject.close()

The object connected to a function (actually called a “method” in this context) using a dot.  What has happened here is that the open() function, when it ran, returned an object.  Part of that object was a method called .close().  Indeed, if you look closely, there are other methods called .write() and .read():

fileObject.write()
          ^
          |----- Can you see the dot?
fileObject.read()
          ^
fileObject.close()
          ^

Methods allow programmers to keep functions and data which they are related to together in one spot and pass them around together like a parcel.  Just as functions which are kept in the object have a special name (methods), so too the data which is kept in the object have a special name – attributes.  So, revisiting the last tutorial (with a little twist because the file was already there – otherwise redo the last tutorial to create it), try this:

>>> import os.path
>>> nameOfFile = 'python4kidsTest.txt'
>>> os.path.exists(nameOfFile)
True
>>> fileObject=open(nameOfFile,'r')
>>> fileObject.name
'python4kidsTest.txt'
>>> fileObject.mode
'r'

Here the object stored in the variable fileObject has two attributes: name and mode.   We provided the data which is stored in these attributes when we called the open() function.  The data stored in these attributes are accessible by dotting them with the name of the object:

fileObject.name
fileObject.mode

I can demonstrate that these are specific to the object by opening another file and having a look at that object:

>>> fileObject2 = open('python4kidsTest2.txt','w')
>>> fileObject2.name
'python4kidsTest2.txt'
>>> fileObject2.mode
'w'

So, even though the attribute is called name, because it is part of an object it stores a data which is specific to that object:  fileObject.name is different from fileObject2.name.  Similarly for the mode attribute of each of these objects.

Objects have a heap of advantages, not all of which are easily explained at the moment. It suffices to say, though, that because they allow packaging of data and functions together, they allow you make programming heaps heaps easier.

Clean up:

fileObject.close()
fileObject2.close()

Confusing bit:

It turns out that everything in Python is an object (which is part of the wonder of Python), even strings:

>>> 'this is a string'
'this is a string'
>>> "it is also an object, see its upper method? ->".upper()
'IT IS ALSO AN OBJECT, SEE ITS UPPER METHOD? ->'
>>>

Keep reading that example until it seems at least a little bit freaky to you.

Homework:

(Subject to the previous observation that everything is an object…) which of the following are functions, which are variables, which are objects, which are attributes (which are constants/literals) and which are methods:

a = 1

b = open(‘testfile.txt’,’r’)

os.path.exists(b.name)

b.close()

Have a look at the earlier tutorials to see where the methods of objects have been used (do you remember .split() and .join()?).