Class Help

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Oct 1 20:44:28 EDT 2005


On Sat, 01 Oct 2005 18:58:45 -0400, Ivan Shevanski wrote:

> To continue with my previous problems, now I'm trying out classes.  But I 
> have a problem (which I bet is easily solveable) that I really don't get.  
> The numerous tutorials I've looked at just confsed me.For intance:

[code snipped]


You have to keep in mind the difference between a class and an instance of
a class. To make an analogy with real life, a class is like the idea of
"dogs in general" and an instance is a specific dog (like Ol' Yella, or
Rin Tin Tin, or or that little beagle on the Enterprise).

Normally you create a class, then make one or more instance of the class,
and work with the instances.

Some terminology for you to learn: when you create a function inside a
class, it is called a class method, or just method. Functions and methods
are not exactly the same, but for now the differences don't concern us.

So, you create a class with a single method:

class Klass:
    def spam(self):
        return "spam " * 5

Notice that the first argument of the method is always "self", even when
you don't need any arguments.

Klass is an object, and you can call Klass.spam() if you like, but it will
fail because you haven't included an argument for self. self must be an
instance of Klass. So you could do this:

spam_maker = Klass()  # create an instance
print Klass.spam(spam_maker)

which will print "spam spam spam " as expected.

But that's doing extra work. Once you have your instance, you can just
call the method as if it were a normal function:

print spam_maker.spam()

and it will work the way you expect it to. Behind the scenes, Python
passes a copy of spam_maker to spam_maker.spam() for you. It can do that
because spam_maker is an instance.

A class is a description of how a type of object should work, but you
normally don't work directly on that high-level description. Normally you
will work with individual instances, not the class itself. When Timmy
falls down the well, you tell Rin Tin Tin to go get help, not "dogs in
the general".

Python built in objects like lists, strings, ints etc are special types of
classes built into the language. Here is how we might create a (very
inefficient!) Python implementation of list:

class MyList:

    # Class attributes:

    left_delimiter = "["
    right_delimiter = "]"
    item_delimiter = ", "

    # Class methods:

    def __init__(self, *arguments):
        """Create a new instance and fill it with arguments."""
        self.data = arguments  # create an instance attribute
    def __str__(self):
        """Convert instance to a string for printing."""
        holder = self.left_delimiter
        for item in self.data:
            holder = holder + str(item) + self.item_delimiter
        holder = holder + self.right_delimiter
        return holder
    def append(self, obj):
        """Append an object to the instance."""
        self.data.append(obj)
    # real lists have many more methods...



Hope this helps.


-- 
Steven.





More information about the Python-list mailing list