Linux/Unix > Python Essential Reference, Second Edition > 7. Classes and Object-Oriented Programming > The class statement | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105086039024252108091154

### The `class` statement

A class defines a set of attributes that are associated with a collection of objects known as instances. These attributes typically include variables that are known as class variables and functions that are known as methods.

Classes are defined using the `class` statement. The body of a class contains a series of statements that are executed when the class is first defined (see Listing 7.1).

##### Listing 7.1 Classes

    class Account:
         "A simple class"
         account_type = "Basic"
         def _ _init_ _(self,name,balance):
             "Initialize a new Account instance"
             self.name = name
             self.balance = balance
         def deposit(self,amt):
             "Add to the balance"
             self.balance = self.balance + amt
         def withdraw(self,amt):
             "Subtract from the balance"
             self.balance = self.balance - amt
         def inquiry(self):
             "Return the current balance"
             return self.balance

The objects created during the execution of the class body are placed into a class object that serves as a namespace. For example, the members of the `Account` class are accessible as follows:

    Account.account_type
    Account._ _init_ _
    Account.deposit
    Account. withdrawAccount.
    inquiry

It’s important to note that a class statement doesn’t create any instances of a class (for example, no accounts are actually created in the preceding example). Rather, a class only defines the set of attributes that are shared by all of the instances that will be created.

The functions defined within a class (methods) always operate on a class instance that’s passed as the first argument. By convention, this argument is called `self`, although any legal identifier name can be used. Class variables such as `account_type` are shared among all instances of a class (that is, they’re not individually assigned to each instance). Although a class defines a namespace, this namespace is not a scope for code appearing inside the class body. Thus, references to other attributes of a class must use a fully qualified name, as shown in the following example:

    class Foo:
        def bar(self):
            print "bar!"
        def spam(self):
            bar(self)     # Incorrect! 'bar' generates a NameError
            Foo.bar(self) # This works

Finally, you cannot define class methods that don’t operate on instances:

    class Foo:
        def add(x,y):
            return x+y

    a = Foo.add(3,4)      # TypeError. Need class instance as first argument

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.