>>> concat(“Hello, “, “world!”)
‘Hello, world!’
That demonstrates how the return statement sends a value back to the caller, but also
how a function can do one thing with lists and another thing with strings. The magic
here is being accomplished by the objects. We write a function that tells two objects to
add themselves together, and the objects intrinsically know how to do that. If they
don’t—if, perhaps, the user passes in a string and an integer—Python catches the error for
us. However, it is this hands-off, “let the objects sort themselves out” attitude that makes
functions so flexible. Our
concat() function could conceivably concatenate strings, lists,
or zonks—a data type someone created herself that allows adding. The point is that we
do not limit what our function can do—tacky as it might sound, the only limit is your
imagination!
Object Orientation
After having read this far, you should not be surprised to hear that Python’s object orien-
tation is flexible and likely to surprise you if you have been using C-like languages for
several years.
The best way to learn Python object-oriented programming (OOP) is to just do it. So, here is
a basic script that defines a class, creates an object of that class, and calls a function:
class dog(object):
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 it 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 the function was called on, 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. Calling a function of
that object is done using a period, the name of the class to call, with any parameters
being passed inside parentheses.
Object Orientation
653
28
Class and Object Variables
Each object has its own set of functions and variables, and you can manipulate those vari-
ables independently of objects of the same type. In addition, some class variables are set
to a default value for all classes and can 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!’
There, 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 we assign that with the setName() method, that reference is lost.
What this means is that any references we do not change can be manipulated globally.
Thus, if we 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
fluffy.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, using
dog.color, we set the default color to be black, and because neither
CHAPTER 28 Working with Python
654

Get Ubuntu Unleashed, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.