[Tutor] Questions about classes

Alan Gauld alan.gauld at btinternet.com
Tue Nov 13 12:08:25 CET 2012


On 13/11/12 02:49, brandon w wrote:

> class HumanBeing:
>          def makeName(self, name):
> *             self.name = name
> *
> *
> Why is it not written like this?:
>
> class HumanBeing:
>          def makeName(self, name):
> *                  name = self.name

Because they two completely different things :-)

The first example takes an argument and assigns it to an attribute
of the object 'self'.

The second takes an argument and assigns the value of the object 
attribute to the argument (which will then get thrown away when the 
function exits)

When we use self.xxx we are accessing or storing that value in an object 
such that its value persists beyond the life of the function.
attributes of objects are a bit like module (or global) level variables 
except they are unique to a specific object. So whereas using global 
variables is considered bad practice using object attributes is good. 
You get the advantages of shared data without the problems of global names.

> 2. Why use a class in the first place? What is the purpose of
> constructing a class instead of just writing a program with a bunch of
> functions?

We write classes as a convenient way of encapsulating functions and data 
that we want to reuse, either within a single project or across 
projects. We reuse them by creating objects. It is the objects that are 
useful, classes are the mechanism for creating objects.

The problem with writing code purely with functions (and data) is the 
management of the data. Its fine if you only have a few data elements 
but when you start processing hundred, thousands or millions of data 
entities keeping them all stored separately and writing functions to 
access the data, making sure you don't corrupt one item while working on 
another becomes a real headache. If you now start parallel processing it 
gets even worse.

Just like modules are a way to control complexity and avoid data 
management issues classes do the same at a finer granularity.

Many people also find thinking about problems in terms of the objects 
involved more intuitive than separating the problem into functions and 
data. The real world is made up of objects that interact so it makes 
sense to build our software the same way. This is more effective in 
programs that model real world entities than in more abstract areas.
But it can be a real benefit in things like GUIs where we have objects 
like  windows, buttons, sliders, menus etc. Having code that reflects 
those visible objects makes GUI development much easier.

Even in the standard Python library we have string objects, file 
objects, and so on. Classes allow us to extend those concepts by 
creating our own objects.

They are optional though. You don't have to use them. You can achieve an 
awful lot without them. But often they make life easier, especially as 
your programs get bigger and more complex.

You'll find more detailed info and examples in the OOP topic of my tutorial.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list