[Tutor] Questions about classes

Dave Angel d at davea.name
Tue Nov 13 09:50:38 CET 2012


On 11/12/2012 09:49 PM, brandon w wrote:
> I have been trying to understand classes. I have been studying from a book
> I picked up recently.
> I have two questions about them.
>
> 1. I saw in the book an assignment written like this:
>
> 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*
> *
> *

Presumably there are also other methods, including an __init__ one. 
That's where initial attributes are (or should be) assigned.

You don't say what else you *do* understand.  So I'll assume you know
what functions are, and how arguments are passed into parameters.  A
class method is very similar, except that it has an extra argument,
normally called 'self', which is usually passed in a funny way.

You know what a list is?  It's an instance of the list class, and it has
certain methods.  Some of those are done with special syntax, like [5],
while others are done with standard method syntax, like the third line
below:
     mylist = list( (3,4) )    #this is usually shortcutted as [3,4]
     item = 42
     mylist.append(item)

mylist is an instance of list, and if you were to look at the class
definition of list, it'd have a method called append.  In this case, the
self parameter of that method refers to mylist (the object), and the
other parameter of that method refers to item.

A class that you write is usually like a collection, with a bunch of
data items (data attributes), and a bunch of methods (method attributes)
to manipulate them.  But unlike a list, they don't have to be uniform --
you define their behavior entirely.   And each time you create an
instance of that class, it gets its own set of attributes.  This
persistence of data between method calls is most of what makes the class
approach more powerful than functions.

Anyway, back to your example.

class HumanBeing:
        def makeName(self, name):
                  *self.name = name*

name is the second parameter, while self.name is an attribute on the current instance.  So if it's the latter you want changed, you'd better have it on the left side of the equals sign.


> 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?
>
>
>

See above.  A class lets you collect multiple characteristics of a
"thing" (object) under one roof, along with the methods to manipulate
them.    For the HumanBeing class, I'd have data attributes for things
like name, address, birthdate, bank_account_num.  And the individual
attributes might change over the life of the object, but they're all
kept together.

If you only had one HumanBeing to deal with in a given program, it
wouldn't matter much.  But if you have a lot of them, trying to use
global variables is very sloppy.

BTW, I wouldn't have a makeName() method, unless I had to start keeping
track of a person (infant) before they got their name.  But I very well
might have a changeName method, to be used at marriage and divorce, or
whenever a person went to court to have it changed.


-- 

DaveA



More information about the Tutor mailing list