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

          |----- Can you see the dot?

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)
>>> fileObject=open(nameOfFile,'r')
>>> fileObject.mode

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:

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.mode

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: is different from  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:


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

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


(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’)



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

3 Responses to Foundations: The Ministry of Silly Objects

  1. Pingback: Links 31/10/2010: SplashTop Updates, Salix KDE Releases, Debian Installer 6.0 @ Beta | Techrights

  2. Pingback: Omigosh: Happy New Year « Python Tutorials for Kids 8+

  3. Emmanuel says:

    Help me with the code: The code don’t open the file, what is wrong in the code?

    import os

    def menu():

    limpiar = ‘cls’ if == ‘nt’else ‘clear’

    print(‘\t1 – Nuevo Archivo’)
    print(‘\t2 – Abrir’)
    print(‘\t3 – Agregar Texto’)
    print(‘\t4 – Nombre y Modo del archivo’)
    print(‘\t9 – Salir’)

    def Inicio():

    while Inicio:




    if opcion == ‘1’:
    nombreArchivo =input(‘Escibe el nombre del archivo: ‘)
    archivo = open(nombreArchivo, ‘w’)
    texto = input(‘Ingrese texto a guardar: \n’)

    elif opcion == ‘2’:
    archivo = open(nombreArchivo, ‘r’)

    elif opcion == ‘3’:

    archivo = open(nombreArchivo, ‘a’)
    texto2 = input(‘Ingrese texto: \n’)

    elif opcion == ‘4’:
    archivo = nombreArchivo
    print(‘Nombre del archivo: \n’),
    print(‘Modo actual del archivo: \n’),archivo.mode

    elif opcion == ‘9’:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: