Programmer's Python - Objects Become Classes
Written by Mike James   
Monday, 04 July 2022
Article Index
Programmer's Python - Objects Become Classes
Instance Attributes
Functions Become Methods

Instance Attributes

We can think of the attributes that the class provides to the instance as class attributes. Notice that all instances of the class initially share the same attributes as they are provided by the class object.

How can the instance acquire its own attributes? The answer is that instance attributes are created when you assign to an instance. In this case the usual rule in Python that assignment creates an attribute or a variable is followed. This means that after assignment the instance has its own attribute. Consider, for example:

myObject=myClass()
print(myClass.myAttribute)
myObject.myAttribute=3
myClass.myAttribute=2
print(myClass.myAttribute)
print(myObject.myAttribute)

In this case after assignment to myObject.myAttribute the instance no longer uses the class attribute and so you see 1, 2, 3 printed. Assigning to the class attribute has no effect on the instance attribute and vice versa.

class2

The rule is that changes to an instance’s dictionary never update the class dictionary. For example, you can delete an instance’s attribute and after this the class attribute of the same name will be used.

You can add attributes to an instance that are not defined in the class. That is, you are not restricted to simply overriding the class attributes. However, adding attributes to an instance that are not defined in the class should be avoided because this spoils the idea that the class is in some way a definition of all of the instances it creates. It is ad-hoc programming and likely to result in a very messy program. These ideas are discussed more in Chapter 7.

Of course, there can be multiple instances of the class and each one can either use the definition of an attribute in the class or can redefine its own version of the attribute.


Defining data attributes in the class is not the best way to do the job as most instances will need their own copies of the attribute i.e. data attributes are generally not shared between instances. The best way of defining data instances is to use the __init__ method which is described later. However, function attributes are best created in the class as they generally shared between instances and the class might as well provide a single copy for all instances.

class3

What Is A Method?

Attributes can be any object, but when attributes are functions things are slightly different. A function that is an attribute of an object created using a class is converted into a method. Before we look at how this works it is worth spending a few minutes looking at the general problem and exactly what a method is as opposed to a function.

When we first started writing programs in higher-level languages, best practice was to write a function for whatever you needed to do. For example, if you needed to sort an array, you would write a sort function which accepted a few parameters that determined the data and the operation:

sort(myArray, order)

where myArray is the data that you want to sort, and order is a parameter that sets the sort order to be used.

Later on we moved over to object-oriented programming where data and the functions that process the data are grouped together into entities called objects. In this case the functions like sort became methods of the data that they were to operate on.

So an Array object would have a sort method and you would write the sort operation as:

myArray.sort(order)

You can see that this is a small change from the use of myArray as a parameter of the sort function to myArray as an object and sort as a method. You could say that the whole of the shift from functions to object-oriented programming is all about the shift of a parameter from inside the function to outside.

Looking a little deeper the simplification that this shift brings about is well worth it. The sort function can now use the data in myArray more or less by default and this makes it possible to create an isolation from the rest of the program. It also brings about a complete change in the way that we think about functions. For example, you could say that myArray “knows how” to sort itself. Another object, myList say, may also “know how” to sort itself using its own sort function which isn’t the same as the array sort function. This means that each data structure can have its own sort function and we can avoid having to have an arraySort function and a listSort function and so on.

This is a limited form of polymorphism and it is one of the huge advantages of object-oriented programming. The idea is that, instead of having lots of sort functions which sort different things, each thing has its own sort method. Now when you write:

myObject.sort(order)

which sort function is called depends on what type of object myObject is. Polymorphism means “many shapes” and in this sense we now have a sort function that comes in different shapes depending on the object it belongs to.

So the key distinction is that:

sort(myArray,order)

is a function that accepts the data it is going to work with as its first parameter whereas:

myArray.sort(order)

is a method that belongs to the myArray object and presumably operates on it.



Last Updated ( Tuesday, 16 April 2024 )