Classes and their resulting objects are often used to represent items in a graphical user interface. For example, we might have classes that represent:
\begin{compactitem}
\item Push button with text
\item Push button with an image
\item Check box
\item Radio button
\end{compactitem}

All of these items have some similarities. In fact, they are similar enough that much of the code will be the same. It does not make sense to repeat this code again and again. Instead, classes can have a ``base'' class that they get common code from. For example:



% \includegraphics{intro_to_classes/button_example.1}


Here are two different class definitions for Critter:

\begin{lstlisting}[]
class Critter():
    name=""
\end{lstlisting}
	
Version 2:

\begin{lstlisting}[]
class Critter(object):
    name=""
\end{lstlisting}

Version 2 class bases its code off a built-in class called ``object.'' If there is no other class that a class should be based off of, it is considered good practice to base the class off ``object.'' This will be discussed more later in class, but since the code in the book uses this convention it needed some explanation.

In the simplest case, a class represents a record. Here's an example of using a class:

\begin{lstlisting}[]
# This class represents a person
class Person:
    name="John Doe"
    age=0
    weight=0
    height=0
\end{lstlisting}

\begin{lstlisting}[]
bob=Person()
bob.name="Bob Smith"
bob.age=22
bob.weight=156
bob.height=60

nancy=Person()
nancy.name="Nancy Smith"
nancy.age=21
nancy.weight=120
nancy.height=55

print (bob.name)
print (nancy.name)
\end{lstlisting}

 
We can also pass an object, and all of its attributes, as a single parameter in a function. It provides a nice way to handle a set of related data. Here is an expanded version of the example above.

\begin{lstlisting}[]
# This class represents a person
class Person:
    name="John Doe"
    age=0
    weight=0
    height=0
    
def print_person(dude):  
    print "Name:   ",dude.name
    print "Age:    ",dude.age
    print "Weight: ",dude.weight
    print "Height: ",dude.height
          
bob=Person()
bob.name="Bob Smith"
bob.age=22
bob.weight=156
bob.height=60

nancy=Person()
nancy.name="Nancy Smith"
nancy.age=21
nancy.weight=120
nancy.height=55

print bob.name
print nancy.name
print

print_person(bob)
print
print_person(nancy)
\end{lstlisting}
 
Definitions:

Class

Object

Instance

Instance variable / attribute

Method / function

Code:
What is special about ``\_\_init\_\_''?


What is special about ``self''?


What is special about a private attribute? How do you create one?
 
Example 2:
\begin{lstlisting}[]
# This class represents a person
class Ball:
    # Initialize the class and set the default values
    def __init__(self):
        self.x=0
        self.y=0
        self.change_x=1
        self.change_y=2
        
    def move(self):
        self.x = self.x+self.change_x
        self.y = self.y+self.change_y
    
def print_ball_details(ball):  
    print "Ball: ",ball.x,",",ball.y
          
ball1 = Ball()
ball2 = Ball()
ball2.x=100
ball2.y=100
ball2.change_x=-1
ball2.change_y=-2

for i in range(5):
    print_ball_details(ball1)
    print_ball_details(ball2)
    print
    ball1.move()
    ball2.move()    
 \end{lstlisting}
Example 3
\begin{lstlisting}[]
import random

# This class represents a person
class Ball:
    # Initialize the class and set the default values
    def __init__(self):
        self.x=0
        self.y=0
        self.change_x=1
        self.change_y=2
        
    def move(self):
        self.x = self.x+self.change_x
        self.y = self.y+self.change_y
    
def print_ball_details(ball):  
    print "Ball: ",ball.x,",",ball.y
          
# Create an empty list
ball_list=[]

# Add 10 balls to the list
for i in range(10):
    # Create the ball
    ball=Ball()
    
    # Set the ball position
    ball.x=random.randrange(1,200)
    ball.y=random.randrange(1,200)
    
    # Set teh ball vector (movement)
    ball.change_x=random.randrange(-5,5)
    ball.change_y=random.randrange(-5,5)
    
    # Add the ball to the list
    ball_list.append(ball)

# Now, run 5 'frames' to move the ball.
for i in range(5):
    # For each ball, move it and print the details
    for ball in ball_list:
        ball.move()
        print_ball_details(ball)
    # Print a blank line.
    print
   
\end{lstlisting}