def bark(self):
print 'Woof!'
fluffy = dog()
fluffy.bark()
Defining a class starts, predictably, with the class
keyword, followed by the name of the class you are defining and a colon. The contents of that class need to be indented one level so that Python knows where each class stops. Note that the object
inside parentheses is there for object inheritance, which is discussed later. For now, the least you need to know is that if your new class is not based on an existing class, you should put object
inside parentheses as shown in the previous code.
Functions inside classes work in much the same way as normal functions do (although they are usually called methods), with the main difference being that they should all take at least one parameter, usually called self
. This parameter is filled with the name of the object on which the function was called, and you need to use it explicitly.
Creating an instance of a class is done by assignment. You do not need any new
keyword, as in some other languages — you just provide empty parentheses. You call a function of
that object by using a period and the name of the class to call, with any parameters passed inside parentheses.
Class and Object Variables
Each object has its own set of functions and variables, and you can manipulate those variables independent of objects of the same type. Additionally, some class variables are set to a default value for all classes and can also be manipulated globally.
This script demonstrates two objects of the dog
class being created, each with its own name:
class dog(object):
name = 'Lassie'
def bark(self):
print self.name + ' says 'Woof!''
def setName(self, name):
self.name = name
fluffy = dog()
fluffy.bark()
poppy = dog()
poppy.setName('Poppy')
poppy.bark()
That outputs the following:
Lassie says 'Woof!'
Poppy says 'Woof!'
Each dog starts with the name Lassie
, but it gets customized. Keep in mind that Python assigns by reference by default, meaning each object has a reference to the class's name
variable, and as you assign that with the setName()
method, that reference is lost. What this means is that any references you do not change can be manipulated globally. Thus, if you change a class's variable, it also changes in all instances of that class that have not set their own value for that variable. For example:
class dog(object):
name = 'Lassie'
color = 'brown'
fluffy = dog()
poppy = dog()
print fluffy.color
dog.color = 'black'
print poppy.color
poppy.color = 'yellow'
print fluffy.color
print poppy.color
So, the default color of dogs is brown — both the fluffy
and poppy dog
objects start off as brown. Then, with dog.color
, the default color is set to black, and because neither of the two objects has set its own color value, they are updated to be black. The third to last line uses poppy.color
to set a custom color value for the poppy
object — poppy
becomes yellow, but fluffy
and the dog
class in general remain black.
Constructors and Destructors
To help you automate the creation and deletion of objects, you can easily override two default methods: __init__
and __del__
. These are the methods called by Python when a class is being instantiated and freed, known as the
Having a custom constructor is great when you need to accept a set of parameters for each object being created. For example, you might want each dog to have its own name on creation, and you could implement that with this code:
class dog(object):
def __init__(self, name):
self.name = name
fluffy = dog('Fluffy')
print fluffy.name
If you do not provide a name
parameter when creating the dog
object, Python reports an error and stops. You can, of course, ask for as many constructor parameters as you want, although it is usually better to ask for only the ones you need and have other functions fill in the rest.
On the other side of things is the destructor method, which allows you to have more control over what happens when an object is destroyed. Using the two, you can show the life cycle of an object by printing messages when it is created and deleted:
class dog(object):
def __init__(self, name):
self.name = name print
self.name + ' is alive!'
def __del__(self):
print self.name + ' is no more!'
fluffy = dog('Fluffy')
The destructor is there to give you the chance to free up resources allocated to the object or perhaps log something to a file.
Class Inheritance