PyClass

Today

Terminology

The class is the template, and the object is what gets built from that template.

Our first class: class Dog

The most basic class:

class Dog(object):
    pass
    

Making a new Dog object:

d = Dog()
print d
    

Constructor:

The constructor gets called when a new object is created.

class Dog(object):
    def __init__(self):
      print "I'm in the constructor!"

d = Dog()

Instance Variables:

class Dog(object):
    def __init__(self, name):
        self.name = name

d = Dog("snowy")
print d.name

Methods:

 
class Dog(object):
    def bark(self):
        print "woof! woof!"

d = Dog()
d.bark()        
      

A To-Do List Application

 
class ListItem(object):
    def __init__(self, value):
        self.value = value